当前位置:网站首页>C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
2022-08-04 04:46:00 【weixin_Guest time】
Five special secrets of function library connection: (the real situation of UNIX link)
1. The extension of dynamic library file is ".so", while the extension of static library file is ".a"
Shared archives have the filename extension ".sa", and shared archives are just a transitional form to help people transition from static libraries to dynamic libraries.
2. For example, through the -lthread option, the compiler is told to link to libthread.so
In fact, the compiler is told to link to the corresponding function library according to the option -lname, and the name of the function library is libname.so--- In other words, the "lib" part and the file extension are omitted, but a "-l" is added in front.
3. The compiler finds the library in a certain directory
It looks in some special locations and looks for the library in /usr/lib.For example, the thread library is located in /usr/lib/libthread.so.
The compiler option -Lpathname tells the linker some other directories. If the -l option is added to the command, the linker will look in these directories for function libraries.The system environment variables LD_LIBRARY_PATH and LD_RUN_PATH are used to provide this information.The use of environment variables is now deprecated for reasons of security, performance, and creating runtime independence.Generally, the -Lpathname and -Rpathname options are used when linking.
4. Observe the header file to confirm the function library used
A good suggestion is to observe the #include directive used by the program.Each header file included in a program may represent a library that must be linked.But the name of the header file is usually not similar to the name of the function library it corresponds to.
The libraries under solaris 2X
#InClude file name The compiler option used in the reference name of the Kulu Lu Road
Another inconsistency with library linking is that the library contains definitions of many functions, but the prototype declarations of these functions
is spread across multiple header files.
The nm command browses all symbols in each library in /usr/lib, looking for missing symbols.By default, the linker will look in /usr/cc/lib and /usr/lib, you should start from these two places too, and expand the search further if you can't find it.(eg /usr/openwin/lib)
%cd /usr/lib
%foreach i (lib?*)
?echo $i
?nm $i | grep xdr_refrence | grep -vUNDEF
?end
...
This runs the nm command on all libraries in this directory, it displays a list of symbols known in the library.Set the symbols to be searched by grep, and filter the symbols marked as UNDEF (referenced in the library, but not defined here).It turns out that xdr_reference is in the libnsl library.Requires -lnsl at the end of the compiler command line.
5. Compared with extracting symbols in dynamic libraries, the method of symbol extraction in static libraries is more restrictive.
In dynamic linking, all library symbols enter the virtual address space of the output file, and all symbolsVisible to all files linked together.In contrast, with static linking, when processing the archive, it just looks in the archive for undefined symbols that the loader knows about at the time.In short, the order in which the various statically linked libraries appear on the compiler command line is very important.Symbols are resolved in left-to-right order.If the same symbol is defined differently in two different libraries, and the static library appears in a different order, the results may be different.
Another problem arises if you import the static library before your own code.Because the undefined symbols have not yet appeared at this point, it does not pull any symbols from the library.Then, when the target problem is handled by the linker, all its references to the library will be unimplemented!
libm often exists as a statically linked archive.If your program uses some mathematical functions such as sin(), etc., if you statically link it like this:
cc -lm main.c
You will get an error message, as follows:
Undefined first referenced
symbol in file
sin main.o
ld:fatal: Symbol referencing errors. No output written to a.out
In order to be able to extract the required symbols from the math library, you first need to make the fileContains unresolved references as follows:
cc main.c -lm
The linker uses the convention
/*Where should the library options be placed*/
Always place the -l library option at the far right of the compile command line
Workspaces can declare functions like the following in the mode that is loaded into the linker, thus providing more clues to the linker:
On the PC, when Borland's compiler driver tries to guess what needs to be linkedThis problem also occurs with floating point numbers.
scanf: floating point formats not linked
abnormal program termination(scanf: floating point formats not linked, program terminated abnormally)
When the program uses floating point formats in scanf() or printf(), butWhen not calling any floating point function, there is a possibility of
guessing wrong
static void forcefloat(float *p) {
float f = *p; forcefloat(&f);
}
You don't need to actually call this function, just make sure it's linked.This gives the Borland PC's linker a sufficiently reliable clue that the floating-point library is indeed needed.
There is also a similar message, when the software requires a numerical coprocessor and the computer does not have it installed, the Microsoft C runtime system prints a message saying "floating point not loaded".This problem can be solved by relinking the program using the floating point number emulation library.
边栏推荐
- 8.Haproxy 搭建Web集群
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
- 图像处理之Bolb分析(一)
- 系统设计.如何设计一个秒杀系统(完整版 转)
- drools from download to postman request success
- 2022 software test interview questions The latest ByteDance 50 real interview questions, 15k have been won after brushing, with explanation + Q&A
- Implementing a server-side message active push solution based on SSE
- C专家编程 第5章 对链接的思考 5.2 动态链接的优点
- centos 安装postgresql13 指定版本
- Reproduce 20-character short domain name bypass
猜你喜欢

使用Loadrunner进行性能测试

Mini program + e-commerce, fun new retail

Oracle与Postgresql在PLSQL内事务回滚的重大差异

manipulation of file contents

JVM Notes

Reproduce 20-character short domain name bypass

How to systematically plan and learn software testing?

基于 SSE 实现服务端消息主动推送解决方案

Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis

Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
随机推荐
PL/SQL Some Advanced Fundamental
8. Haproxy builds a web cluster
帮助企业实现数字化转型成功的八项指导原则
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
【id类型和NSObject指针 ObjectIve-C中】
7-2 LVS+DR Overview and Deployment
mysql索引笔记
3000字,一文带你搞懂机器学习!
10 Convolutional Neural Networks for Deep Learning 3
如何动态添加script依赖的脚本
Learn iframes and use them to solve cross-domain problems
2022年PMP考试延迟了,该喜该忧?
软件测试如何系统规划学习呢?
【21天学习挑战赛】图像的旋转问题(二维数组)
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
【机器学习】21天挑战赛学习笔记(一)
The Shell function
Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
基于gRPC编写golang简单C2远控
【21天学习挑战赛】直接插入排序