We know that in linux, The. so file is equivalent to the dll file on windows, that is, the dynamic link library.
The dynamic link library is used to reduce the size of the release program. code with the same function can be placed in the dynamic link library and released along with the application. For an application, you only need to know its interface to dynamically load code to the memory at runtime. Instead, the static link library is used.
After taking over the code of a project, because the Code requires many third-party libraries, after installing these third-party libraries on the local machine, I also successfully passed the compilation and link phase, generated executable code.
However, the following error message is displayed during running:
baiyang@baiyang-Lenovo-G450:~/Desktop$ ./test_cal_features./test_cal_features: error while loading shared libraries: libCGAL.so.5: cannot open shared object file: No such file or directory
Several questions are involved:
1. Why can't I run it through compilation and link?
2. I installed libCGAL. so.5 to/usr/local/lib. Since it can be found through compilation and link?
3. What happened when I executed./test_cal_features?
4. in linux, how does an application search for. so?
But the essential problem is the gcc search path setting in linux.
Well, I will answer the above questions one by one today.
Start a new example
Plus. c file
View plaincopy to clipboardprint?
- Int plus (int a, int B)
- {
- Return a + B;
- }
Compiled into a dynamic link library
gcc plus.c -o libfoo.so -shared -fPIC
Generate dynamic link library libfoo. so
Write another main. c file
View plaincopy to clipboardprint?
- # Include <stdio. h>
- # Include <stdlib. h>
- Int main (int argc, char * argv [])
- {
- Int sum = plus (3, 5 );
- Printf ("% d \ n", sum );
- Return 0;
- }
Now I am thinking about how to store the libfoo. so location?
First, we store libfoo. so in the current directory for compilation and linking.
Gcc main. c-o output-lfoo //-lfoo tells gcc that we need to dynamically link the foo library.
The following prompt is displayed:
baiyang@baiyang-Lenovo-G450:~/workspace/test_so$ lslibfoo.so main.c plus.cbaiyang@baiyang-Lenovo-G450:~/workspace/test_so$ gcc main.c -o main -lfoo/usr/bin/ld: cannot find -lfoocollect2: ld returned 1 exit status
That is to say, gcc cannot find the foo library. If we show instructions on how gcc searches for foo, we can modify the value of LIBRARY_PATH.
baiyang@baiyang-Lenovo-G450:~/workspace/test_so$ export LIBRARY_PATH="."baiyang@baiyang-Lenovo-G450:~/workspace/test_so$ gcc main.c -o main -lfoobaiyang@baiyang-Lenovo-G450:~/workspace/test_so$ lslibfoo.so main main.c plus.c
Congratulations! We found it successfully.
Well, if we run the code
baiyang@baiyang-Lenovo-G450:~/workspace/test_so$ ./main./main: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
Hey, I encountered the problem I mentioned at the beginning.
So how do we see how to read libfoo. so from the output file main? What files are read during the running process?
We can use the strace command to check whether any signal has occurred.
strace ./main
You will find that the search path is
/Lib/i386-linux-gnu,/usr/lib and other path directory
That is to say, because the current directory is not searched, We can modify it by modifying the following command.
baiyang@baiyang-Lenovo-G450:~/workspace/test_so$ export LD_LIBRARY_PATH=".":$LD_LIBRARY_PATHbaiyang@baiyang-Lenovo-G450:~/workspace/test_so$ lslibfoo.so main main.c plus.cbaiyang@baiyang-Lenovo-G450:~/workspace/test_so$ ./main8
Run the statement "open ("./libfoo. so ", O_RDONLY | O_CLOEXEC) = 3" with strace.
Therefore, the current directory is searched.
To sum up, we have mentioned several environment variables:
- LIBRARY_PATH this environment variable can be set to a list of one or more directory names. The Connection Program searches for this directory to find the special connection program file, and by-l (letter l) the command line option specifies the database name. The directory specified by the-L command line option is first located before the environment change. See COMPILER_PATH.
- LD_LIBRARY_PATH this environment variable does not affect the Compilation Program, but will affect the running of the program. The variable specifies a directory list, and the program searches for the list to locate the shared library. This variable must be set only when the shared library is not found in the compiled program directory.
By default, how does one search?
The search path sequence of a dynamic library is as follows:
- 1. The dynamic library search path specified when the target code is compiled. LIBRARY_PATH [compilation phase ];
- 2. At runtime, the dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH [runtime stage ];
- 3. The dynamic library search path specified in the configuration file/etc/ld. so. conf [compilation phase ];
- 4. Default dynamic library search path/lib [compilation phase ];
- 5. The default dynamic library search path/usr/lib [compilation stage ].
Process binary files
The above tools allow you to view what the binary file actually does. For example, we can use objdump to view the Link Library required for the executable file.
baiyang@baiyang-Lenovo-G450:~/workspace/test_so$ objdump -x main | grep NEEDNEEDED libfoo.soNEEDED libc.so.6VERNEED 0x08048354VERNEEDNUM 0x00000001
If you are familiar with them, you will have a better understanding of the system. Come on.
Additional information
- Describes in detail how gcc searches for the correct file and link library;
- Strace
----------------- Create high-quality articles. pay more attention to the wine ---------------
To create high-quality articles, please recommend them .... Thank you. Please pay attention to my subsequent articles, which will be even better.
Sina Weibo: http://weibo.com/baiyang26
The wine ghost official blog: http://www.ibaiyang.org [recommended to use google reader subscription]
Pour wine into the official Douban: http://www.douban.com/people/baiyang26/
If you want to repost this blog,Please specify the source
If you have any comments or suggestions on this article,Leave a message