Linux GCC link Dynamic library error: Difference between Library_path and Ld_library_path

Source: Internet
Author: User

When I wrote makefile on my CentOs7.1 yesterday, I found that a C program made an error when compiling and linking a generated LIB dynamic library. The link command is probably like this:

[[email protected] tcpmsg]# gcc -o hello main.c -lmyhello/usr/bin/ld: cannot find -lmyhellocollect2: error: ld returned 1 exit status

1 The search path when GCC links a dynamic library

I think the environment variable LD_LIBRARY_PATH contained the Lib library path in the project, and the /etc/ld.so.conf/apphome.conf path of Lib library is also configured in the current project. Then, when invoking the dynamic library, GCC should be able to automatically search for that directory.

Unfortunately, the LD Linker reported the error above, but if you add a parameter to the GCC command above, that is, the -L /root/gcc_test/tcp_msg/lib/ absolute path of the dynamic library can be linked successfully.

2 The cause of the error in finding/usr/bin/ld:cannot find-l* on google

GG for a long time the reasons for the failure of the GCC LD link Dynamic Library, the results still did not find the ideal answer. Later speculation is not in the CentOs7 Ld_library_path, but should not, because their own gcc (version 4.8.3) with the operating system does not matter. then re-searched the role of GCC Ld_library_path, actually found that GCC in the compilation link when the link to the dynamic library with the LIBRARY_PATH relevant and it LD_LIBRARY_PATH doesn't matter!

3 about LIBRARY_PATH and parameter descriptions in Linux LD_LIBRARY_PATH gcc

The following extracts two more authoritative explanatory materials:

1. The GNU Note on Library_path:

 library_paththe value of library_path is A colon-separated list of directories, much like PATH.when configured as a native compiler, GCC tries the directories thus specified when searching FOR special linker files, if it can T Find them using Gcc_exec_prefix. Linking using GCC also uses these directories when searching Span class= "Hljs-keyword" >for Ordinary libraries for the-l  Option (but directories specified with-l come first).      

2, Man7 on the Ld_library_path description:

of directories in which to search forELF libraries at execution-time.  Similar to the PATHenvironment variable. Ignored in set-user-ID and set-group-IDprograms.

Later found on the StackOverflow on LIBRARY_PATH and LD_LIBRARY_PATH the explanation more straightforward:

Library_pathis usedby GCC before compilationTo searchFor directories containing libraries that needTo be linkedto your program. Ld_library_pathis usedBy your ProgramTo searchFor directories containing the libraries after it had been successfully compiledand linked. EDIT:As pointed below, your libraries can bestatic or shared.if it is static  Then the code is copied over into Your program and you don T need to search for the library after your program is compiled and L Inked. if your library is shared then it needs to be dynamically linked to Your program and that s when Ld_library_path comes into play.
                                                    

Through the three information of the description, quickly understand LIBRARY_PATH and LD_LIBRARY_PATH the role. Then, you add them in the project configuration file export LIBRARY_PATH=${LIBRARY_PATH}:${APPHOME}/lib . This configuration file is then loaded into the CENTOS environment variable, so that the GCC compiler does not have to add -L parameters to generate the target file commutcp.

4 Summary

About LIBRARY_PATH LD_LIBRARY_PATH the relationship, and here to summarize their own.

4.1 Dynamic Library search path for Linux gcc when compiling links

When GCC is compiled and chained to the executable, the search path order of the dynamic library is as follows (note that it does not recursively search under its subdirectories):

1、gcc编译、链接命令中的-L选项;2、gcc的环境变量的LIBRARY_PATH(多个路径用冒号分割);3、gcc默认动态库目录:/lib:/usr/lib:usr/lib64:/usr/local/lib。

4.2 Dynamic Library search paths when binary files are executed

After the link is delivered to the binary executable, the search path order is as follows when the program loads the dynamic library file:

1、编译目标代码时指定的动态库搜索路径:用选项-Wl,rpath和include指定的动态库的搜索路径,比如gcc -Wl,-rpath,include -L. -ldltest hello.c,在执行文件时会搜索路径`./include`;2、环境变量LD_LIBRARY_PATH(多个路径用冒号分割);3、在 /etc/ld.so.conf.d/ 目录下的配置文件指定的动态库绝对路径(通过ldconfig生效,一般是非root用户时使用);4、gcc默认动态库目录:/lib:/usr/lib:usr/lib64:/usr/local/lib等。

Among them, the Linux gcc default dynamic library search path can be viewed by the ld --verbose command:

[Email protected] tcpmsg]#ld--verbose .......  Search_dir ("/usr/x86_64-redhat-linux/lib64");  Search_dir ("/usr/local/lib64");  Search_dir ("/lib64");  Search_dir ("/usr/lib64"); ##### 64-bit system Search_dir ("/usr/x86_64-redhat-linux/lib");  Search_dir ("/usr/local/lib");  Search_dir ("/lib");  Search_dir ("/usr/lib");                

Transferred from: http://typecodes.com/cseries/gcclderrlibrarypath.html

Linux GCC link Dynamic library error: Difference between Library_path and Ld_library_path

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.