知识点
知识点2
知识点3
实例
//a.cc == main.cc
#include <iostream>
#include "b.h"
int main()
{
bbb();
std::cout << "Hello world" << std::endl;
return 0;
}
//b.h-----
#pragma once
void bbb();
//b.cc-----
#include <iostream>
#include "ccc.h"
void bbb()
{
ccc();
std::cout << "Hello world" << std::endl;
}
// ccc.h-----
#pragma once
void ccc();
// ccc.cc-----
#include <iostream>
void ccc()
{
std::cout << "Hello world" << std::endl;
}
编译过程1
$ g++ -fPIC -shared ccc.cc -o libccc.so
$ g++ -fPIC -shared b.cc -o libbbb.so -L. -lccc
# g++ a.cc -L. -lb 是不行的,具体看https://blog.csdn.net/zrq293/article/details/105969423
g++ a.cc -L. -lb -Wl,--copy-dt-needed-entries
/usr/bin/ld: warning: libccc.so, needed by ./libb.so, not found (try using -rpath or -rpath-link)
$ g++ a.cc -L. -lb -Wl,--copy-dt-needed-entries,-rpath-link=.
$ ./a.out
./a.out: error while loading shared libraries: libb.so: cannot open shared object file: No such file or directory
$ g++ a.cc -L. -lb -Wl,--copy-dt-needed-entries,-rpath-link=.,-rpath=.
tiantian@DESKTOP-UVN3KRD:~/code$ ./a.out
./a.out: error while loading shared libraries: libccc.so: cannot open shared object file: No such file or directory
$ ldd a.out
linux-vdso.so.1 (0x00007ffff5585000)
libb.so => ./libb.so (0x00007f44961f0000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4495fc0000)
libc.so => ./libc.so (0x00007f4495fb0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4495d80000)
libccc.so => not found
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4495c80000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4496215000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4495c60000)
$ export LD_LIBRARY_PATH=.
$ ./a.out
Hello world
Hello world
Hello world
cmake的扩展