源文件:windows
//world.cpp #include <stdio.h> void world(void) { printf("world.\n"); }
//hello.cpp #include <stdio.h> void world(void); void hello(void) { printf("hello\n"); world(); }
//test.cpp void hello(void); int main(void) { hello(); return 0; }
1、动态库多重依赖spa
(1)编译word动态库3d
g++ -shared -fPIC world.cpp -o libworld.socode
(2)编译hello动态库blog
g++ -shared -fPIC hello.cpp -o libhello.soio
ldd libhello.so编译
查看libhello.so的依赖库,没有看到依赖libword.soclass
g++ -shared -fPIC hello.cpp -o libhello.so -L ./ -lworldtest
ldd libhello.sobfc
再次查看libhello.so的依赖库,看到了依赖库libword.so
上图显示libworld.so not found,若是临时增长连接动态库的路径,输入以下命令
export LD_LIBRARY_PATH=./
ldd libhello.so
查看libhello.so的依赖库,显示了依赖库libword.so的路径
先清除连接动态库路径
export LD_LIBRARY_PATH=
(3)编译可执行文件test
g++ test.cpp -o a.out -L ./ -lhello
提示找不到libhello.so的依赖库libworld.so,即便编译libhello.so时已经指定了libworld.so,这点和windows不同
g++ test.cpp -o a.out -L ./ -lhello -lworld -Wl,-rpath ./
编译经过,获得可执行文件a.out,运行成功
2、动态库静态库多重依赖
(1)编译word静态库
g++ -c world.cpp
ar -cr libworld.a world.o
(2)编译hello动态库
g++ -shared -fPIC hello.cpp -o libhello.so -L ./ -lworld
编译报错,由于world也必须使用-fPIC,从新编译
(3)编译可执行文件
g++ test.cpp -o a.out -L ./ -lhello -Wl,-rpath ./
3、静态库多重依赖
(1)编译world静态库
g++ -c world.cpp
ar -cr libworld.a world.o
(2)编译hello静态库
g++ -c hello.cpp
ar -cr libhello.a hello.o
(3)编译可执行文件
g++ test.cpp -o a.out -L ./ -lworld -lhello
由于静态库的依赖有顺序,被调用库应该放在调用库后面,动态库没有依赖顺序,正确输入以下
g++ test.cpp -o a.out -L ./ -lhello -lworld
(4)静态库包含静态库