ld 是 Linux 系统中的链接器,用于将一个或多个目标文件(object files)链接成一个可执行文件
-o 或 --output:指定输出文件的名称。例如:ld -o output_file source_file1.o source_file2.o
-s 或 --strip-all:移除所有符号信息,使生成的可执行文件更小。例如:ld -s -o output_file source_file.o
-static:静态链接,将所有依赖的库文件嵌入到生成的可执行文件中。例如:ld -static -o output_file source_file.o
-shared:动态链接,生成一个动态链接库文件(共享库),而不是可执行文件。例如:ld -shared -o libshared.so source_file.o
-fPIC:生成位置无关代码(Position Independent Code),使得生成的共享库可以在任何内存地址运行。通常与 -shared 一起使用。例如:gcc -c -fPIC source_file.c
ld -shared -o libshared.so *.o
-L 或 --library-path:指定库文件的搜索路径。例如:ld -L/path/to/library -o output_file source_file.o
-l 或 --library:链接指定的库文件。例如:ld -lmylib -o output_file source_file.o
-Wl, 或 --dynamic-linker:指定动态链接器的路径。通常不需要设置,因为系统会自动使用合适的动态链接器(如 /lib/ld-linux.so.2)。例如:ld -Wl,/path/to/dynamic-linker -o output_file source_file.o
-rpath 或 --runpath:指定程序运行时库文件的搜索路径。例如:ld -rpath=/path/to/library -o output_file source_file.o
这只是 ld 命令的一部分参数,还有其他参数可用于控制链接过程。要查看完整的参数列表,请参阅 ld(1) 手册页。