-
–fPIC
fPIC 的全称是 Position Independent Code, 用于生成位置无关代码。
-fpic
Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)
Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.
When this flag is set, the macros __pic__ and __PIC__ are defined to 1.
-fPIC
If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on AArch64, m68k, PowerPC and SPARC.
Position-independent code requires special support, and therefore works only on certain machines.
When this flag is set, the macros __pic__ and __PIC__ are defined to 2.
-
openssl库
解压 openssl-OpenSSL_0_9_8zd.zip
解压后 执行 ./config
修改Makefile文件
CFLAG=… -fPIC
在后面增加 -fPIC
然后直接make即可
生成 两个库 libssl.a libcrypto.a
-
Tommath库编译说明
解压 ltm-1.0.1.zip
修改Makefile文件
CFLAG=… -fPIC
在后面增加 -fPIC
然后直接make即可
生成 libtommath.a
-
USB驱动说明
将下载 libusb-1.0.20-rc3.tar.bz2的压缩包拷贝到libusb(或任意目录)目录下,tar xjvf libusb-1.0.20-rc3.tar.bz2解压
电脑是 x86_64, 因此命令是: ./configure --build=x86_64-linux --disable-udev --enable-static
电脑是 aarch64, 因此命令是:
./configure --build=aarch64 --host=arm-linux --disable-udev --enable-static
修改Makefile 以及libusb目录的Makefile
修改增加(包括c,cpp,am等)
CFLAG=… -fPIC
执行命令, make install
如果没有权限,可以执行
可以执行
./libtool --mode=install /usr/bin/install -c libusb/libusb-1.0.la '/home/xx/local/lib'
生成 libusb-1.0.so
-
用户库编译
编译.o文件
$(CC) -fPIC -c $<
编译库
$(CC) -shared -fPIC -o $@ $^ -ldl -l:libcrypto.a -l:libssl.a -l:libtommath.a -l:libusb-1.0.so
或者
$(CC) -shared -fPIC -o $@ $^ -ldl -l:libcrypto.a -l:libssl.a -l:libtommath.a -l:libusb-1.0.a -pthread
问题:undefined reference to `pthread*
用-l:libusb-1.0.a(带fPIC编译出来的库)来联编,最后在可执行文件出现一下错误
undefined reference to `pthread_mutexattr_settype'
undefined reference to `pthread_mutex_trylock'
undefined reference to `pthread_join'
undefined reference to `pthread_create'
undefined reference to `pthread_mutexattr_init'
undefined reference to `pthread_mutexattr_destroy'
在可执行文件编译时,需要带上 -pthread 即可.
或者在编译库时带上
$(CC) -shared -fPIC -o $@ $^ -ldl -l:libcrypto.a -l:libssl.a -l:libtommath.a -l:libusb-1.0.a -pthread
undefined reference to `clock_gettime'
添加 -lrt
符号可见性
readelf -a X.so
GLOBAL的符号即全局的符号
符号的可见性(Symbol Visibility)问题,有GLOBAL符号,有LOCAL符号,LOCAL的符号只在当前lib可见,全局不可见。
编译参数需要加上 -fvisibility=hidden ,加了这个选项后,默认的符号都不会导出
在需要导出的函数或者类名前加 __attribute__ ((visibility ("default")))
在编译动态库的是加参数-Wl,-Bsymbolic 这个参数是传给链接器的,这个编译参数的作用是:优先使用本动态库中的符号,而不是全局符号。
-Wl,--exclude-libs,ALL 这个选项的作用是隐藏依赖的静态库符号
CFLAGS=-fPIC -fvisibility=hidden
$(CC) -shared -fPIC -Wl,--exclude-libs,ALL,-Bsymbolic -o $@ $^ ...
C程序链接C++库报错问题
使用gcc最后生成可执行文件时,如果所依赖的库文件是C++写的,必须加上-lstdc++选项,否则会报一大堆C++的错误,比如:
undefined reference to `operator delete(void*)'
undefined reference to `operator new(unsigned int)'