做者:施洪宝html
三、g++参数说明,java
四、连接器ld参数:linux
一、对于c或者c++项目而言, 咱们认为单个c或者cpp文件是一个编译单元, 经过编译器(gcc, g++, clang, clang++)能够生成编译后的二进制文件。例如: 编译file1.cpp, 能够生成file1.o。对于单个编译单元而言, 里面会有一些符号, 例如函数名称, 变量名称, 类名。这些符号能够分为三类:ios
#ifndef _MATH_H_ #define _MATH_H_ int add(int a, int b); #endif
#include "math.h" int add(int a, int b){ return a + b; }
#include <iostream> #include "math.h" using namespace std; int main(int argc, char **argv){ int a = 100, b = 200; int result = add(a, b); cout << result << endl; }
四、生成可执行文件c++
Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/ LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /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 -o main /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. main.o math.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o
经过上面的介绍, 咱们知道一个c/cpp文件经过编译连接, 最终生成可执行文件。不管任何语言, 程序员在写代码时, 都不可避免须要使用到库, 本文主要介绍C/C++中的库, 整体而言, 咱们将这些库分为静态连接库(一般以.a结尾),动态连接库(一般以.so结尾)。首先咱们来看几个问题:程序员
本节以hello world为例,shell
#include <iostream> using namespace std; int main(int argc, char **argv){ cout << "hello world" << endl; }
linux-vdso.so.1 => (0x00007ffcf53fa000) libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f7828b3b000) libm.so.6 => /lib64/libm.so.6 (0x00007f7828839000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f7828623000) libc.so.6 => /lib64/libc.so.6 (0x00007f7828256000) /lib64/ld-linux-x86-64.so.2 (0x00007f7828e42000)
一、编译: g++ -std=c++11 -fPIC -c math.cppbootstrap
三、使用这个静态连接库:windows
二、使用动态连接库:app
./main: error while loading shared libraries: libmath.so: cannot open shared object file: No such file or directory
linux-vdso.so.1 => (0x00007ffd2adde000) libmath.so => not found libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fd3b7ee6000) libm.so.6 => /lib64/libm.so.6 (0x00007fd3b7be4000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd3b79ce000) libc.so.6 => /lib64/libc.so.6 (0x00007fd3b7601000) /lib64/ld-linux-x86-64.so.2 (0x00007fd3b81ed000)
很奇怪, libmath.so没有找到, 咱们在第三步编译时明明将这个库加入进去了。这个是因为, 在连接阶段, 连接器能够在当前目录找到libmath.so。执行阶段, 搜索动态连接库时, 并无包含当前目录, 因此报错。咱们能够经过export LD_LIBRARY_PATH=/libpath将libmath.so所在路径放入动态连接库的搜索路径中。此时便可成功执行。
从静态连接库生成的过程来看, 其本质就是将多个编译单元(.o文件), 打包为一个新的文件。连接静态连接库时, 会将静态连接库的代码合并进程序中。
#ifndef __FIRST_H_ #define __FIRST_H_ #include <cstdio> void first(); #endif
#include"first.h" void first() { printf("This is first!\n"); }
#ifndef __SECOND_H_ #define __SECOND_H_ #include <cstdio> void second(); #endif
#include"first.h" #include"second.h" void second() { printf("This is second!\n"); first(); }
#include"second.h" int main() { second(); return 0; }
g++ -std=c++11 -fPIC -c first.cpp ar -crv libfirst.a first.o
g++ -std=c++11 -c second.cpp -L. -lfirst ar -crv libsecond.a second.o
执行: g++ -std=c++11 main.cpp -L. -lsecond -o main
会出现如下错误:
./libsecond.a(second.o): In function second()': second.cpp:(.text+0xf): undefined reference tofirst()’ collect2: error: ld returned 1 exit status
四、解释说明
五、解决方案
同时连接libsecond.a, libfirst.a
g++ -std=c++11 second.cpp -fPIC -shared -o libsecond.so -L. -lfirst
g++ -std=c++11 main.cpp -L. -lsecond -o main
g++ -std=c++11 first.cpp -shared -fPIC -o libfirst.so
g++ -std=c++11 -c second.cpp -fPIC -L. -lfirst ar crv libsecond.a second.o
nm -u libsecond.a, 能够看到_Z5firstv, 说明并无将libfirst.so中包含进libsecond.a
g++ -std=c++11 main.cpp -L. -lsecond -lfirst -o main
若是没有连接first, 会发现连接错误, 找不到first函数的定义
g++ -std=c++11 first.cpp -shared -fPIC -o libfirst.so
g++ -std=c++11 second.cpp -shared -fPIC -o libsecond.so -L. -lfirst
nm -u libsecond.so, 能够看到_Z5firstv, 这个就是first函数
ldd libsecond.so, 也能够看到libfirst.so
能够看出, 使用libsecond.so时, 仍然须要libfirst.so
g++ -std=c++11 main.cpp -L. -lsecond -o main
能够看出, 可以成功编译。
以前讲过libsecond.so须要依赖libfirst.so, 此处为什么咱们只连接libsecond.so也能成功呢?这里是由于连接器会自动搜索动态连接库的依赖库
一、动态库升级问题?假设如今有2个程序: p1, p2, 一个动态连接库libmath.so.1。若是如今math库提供了新版本libmath.so.2, 程序p1须要使用libmath.so.2的新功能, p2则不想使用, 此时该如何升级math库?
二、某个动态连接库lib1动态连接了库libbase, 如今应用程序中使用了lib1以及libbase, 编译应用程序时, 是否须要连接libbase?
三、菱形依赖问题, A依赖于B以及C, B、C都依赖于D, 可是是不一样版本, 例如B依赖于D1, C依赖于D2, 这种状况下如何连接?
连接器的参数, 直接连接两个版本。ld的参数–default-symver或者–version-script
四、讨论
http://blog.chinaunix.net/uid...
https://www.cnblogs.com/fnlin...
https://blog.csdn.net/coolwat...
https://blog.habets.se/2012/0...