The ld command in linux and its search path order

1. ld command

1.1. Introduction

The ld (Link eDitor) command is a member of the binary tool set GNU Binutils and is a GNU linker used to link object files and libraries into executable or library files.

The ld command supports many link options, but most of the options are rarely used, common parameters:

-o	指定输出文件名
-e	指定程序的入口符号

1.2. Example of use

(1) Link object files to generate executable files . For example, given the C++ object files test.o and main.o, generate the executable file test.out. Note that many system library files need to be linked in this process, so the writing method is very complicated. This is why the ld command is rarely used.

ld /usr/lib64/crt1.o /usr/lib64/crti.o /usr/lib64/crtn.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib64 -L/usr/lib -lstdc++ -lm -lgcc_s -lc -lgcc  main.o test.o -o test.out

(2) Because generating a C++ executable file needs to rely on many system libraries and related target files, such as the C language library libc.a, when using ld to link, you need to pay attention to adding a longer command option, otherwise the link will be reported mistake. Use g++ -vthe command to view the dependencies required to generate an executable.

g++ -v main.o test.o
...
usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o /usr/lib64/crtn.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib64 -L/usr/lib -lstdc++ -lm -lgcc_s -lc -lgcc  main.o test.o

2, ld command search path order

2.1. Search path order

Note : The search order mentioned below should be correct, but there may be some errors in other content. For example LIBRARY_PATH, LD_LIBRARY_PATHare they static libraries and dynamic libraries respectively? In addition, the default search path of the library is more than /lib /usr/lib /usr/local/libthese.

1) Search path order when linking static libraries

  1. ld will look for the parameter -L in the GCC command
  2. Find the environment variable LIBRARY_PATH of gcc
  3. Then find the default directory /lib /usr/lib /usr/local/lib which was written in the program when compiling gcc 

2) Search path order during dynamic linking and execution:

  1. The dynamic library search path specified when compiling the object code
  2. The dynamic library search path specified by the environment variable LD_LIBRARY_PATH
  3. The dynamic library search path specified in the configuration file /etc/ld.so.conf
  4. The default dynamic library search path /lib 
  5. The default dynamic library search path /usr/lib  

3) Related environment variables

LIBRARY_PATH environment variable: specify the program static link library file search path

LD_LIBRARY_PATH environment variable: specify the program dynamic link library file search path (LD: library dynamic)

2.2. How to view the search order of ld (linker) and the libraries that need to be opened during compilation

2.2.1. View the default search path of ld

# 1. 把ld命令脚本的所有内容输入到ld_path文件中
ld --verbose | tee ~/ld_path.txt

# 2.仅仅过滤其中SEARCH有关的部分显示
ld --verbose | grep SEARCH

Among them, ld --verbose | grep SEARCHthe display results are as follows, and you can see that not only lib /usr/lib uar/local/libthe etc. directory is included, but also other directories.

2.2.2. View the libraries that need to be opened when compiling

Suppose there is a file dummy.cppwith the following content:

#include <iostream>

int main()
{

}

Use g++ dummy.cpp -Wl,--verbose | grep succeeded, or if the system language is Chinese, enter g++ dummy.cpp -Wl,--verbose | grep 成功to view the library opened during compilation. The result is as follows:

Reposted from: The ld command in linux and its search path sequence_linux ld_Cc1924's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/fuhanghang/article/details/130385487