gcc link链接常用选项及应用
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033
文章目录
相关问题:
1.跨操作系统程序编写等应用
2.通用linux应用程序
环境:
OS : Centos 7
编译器: g++ 4.8.5
1.前言
gcc的link选项只有在目标文件链接到可执行文件时,link选项才生效。
了解link选项,有利于可执行文件的深入理解。例如,跨操作系统程序编写等应用。
2.常用link选项
2.1 -flinker-output=type
重要程度(1 - 5):1
此选项控制链接时间优化器的代码生成。默认情况下,链接器输出由链接器插件自动确定。
2.2 -llibrary
(-l library
)(★)
重要程度(1 - 5):5
链接时搜索名为library的库
链接器在标准目录列表中搜索该库。搜索的目录包括几个标准系统目录以及-L
指定的任何目录
静态库文件名类似于liblibrary.a
。共享库文件名类似于liblibrary.so
。如果同时找到了静态库和共享库,则链接器将优先选择与共享库链接,除非的使用 -static
选项
2.3 PIE(Position Independent Executable)
重要程度(1 - 5):3
PIE能用来创建介于共享库和通常可执行代码之间的代码–能像共享库一样可重分配地址的程序
- -pie
生成位置无关的可执行文件。通常配合-fpie
,-fPIE
使用 -no-pie
不生成位置无关的可执行文件-static-pie
生成位置无关的静态可执行文件。通常配合-fpie
,-fPIE
使用
2.4 -pthread
(★)
重要程度(1 - 5):5
与POSIX线程库链接。GNU / Linux、Unix以及x86 Cygwin和MinGW目标都支持此选项。
gcc -o simple simple.c -lpthread
2.5 动态链接库和静态链接库(★)
重要程度(1 - 5):5
-shared
产生一个共享对象,然后可以将其与其他对象链接以形成可执行文件。需要配合-fpic
,-fPIC
使用
gcc -fPIC -shared hello.c -o libhello.so
-static
生成静态可执行文件。
gcc main.c -lhello -L. -static -o main
2.6 libgcc
(★)
重要程度(1 - 5):5
-shared-libgcc
链接动态libgcc库
-static-libgcc
链接静态libgcc库
g++ -o hello hello.cpp -static-libgcc -static-libstdc++
2.7 -static-libstdc++
(★)
重要程度(1 - 5):5
当g++程序用于链接C ++程序时,通常会自动链接到libstdc++
。如果 libstdc++
可以作为共享库使用,并且-static
的 选项未使用,则此链接指向的共享版本 libstdc++
g++ -o hello hello.cpp -static-libgcc -static-libstdc++
2.8 其他
-e entry
-lobjc
-nodefaultlibs
-nostdlib
-nolibc
-r
-rdynamic
-s
-static-libasan
-static-libtsan
-static-liblsan
-static-libubsan
-symbolic
-T script
-Xlinker option
-Wl,option
-u symbol
-z keyword
3.应用
// hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
return 0;
}
- 普通编译
$ g++ -o hello hello.cpp
$ ./hello
hello world
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=cef26c0f4878e3835d81a548fcca0a61701ec14a, not stripped
$ ldd hello
linux-vdso.so.1 => (0x00007fff9019e000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f9f48cf7000)
libm.so.6 => /lib64/libm.so.6 (0x00007f9f489f5000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f9f487df000)
libc.so.6 => /lib64/libc.so.6 (0x00007f9f48412000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9f48ffe000)
- 静态编译
$ sudo yum install glibc-static libstdc++-static
$ g++ -o hello hello.cpp -static
$ ./hello
hello world
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=ab5abd481d857b3f35e7514e687993717bd5c742, not stripped
$ ldd hello
不是动态可执行文件
- 半静态编译
$ sudo yum install glibc-static libstdc++-static
$ g++ -o hello hello.cpp -static-libgcc -static-libstdc++
$ ./hello
hello world
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=89c7bf8e8f31d34f7c5ac899264b7fe069ec2108, not stripped
$ ldd hello
linux-vdso.so.1 => (0x00007fff20d4b000)
libm.so.6 => /lib64/libm.so.6 (0x00007f800fee0000)
libc.so.6 => /lib64/libc.so.6 (0x00007f800fb13000)
/lib64/ld-linux-x86-64.so.2 (0x00007f80101e2000)
License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033
Reference:
1.https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html