复制和粘贴.so文件不适用于链接器

时间:2014-11-06 06:29:52

标签: c++ linux c++11 dynamic-library casablanca

我在我的主目录中编译并构建了casablanca c ++ rest库,其中我所需的.so文件的绝对路径为/home/dev/casablanca/Release/build.release/Binaries/libcpprest.so。我想要做的只是简单地cp并将.so文件传递到/usr/lib/..路径到默认的lib搜索../,以便我可以轻松地将它与以下命令链接:

g++ index.cpp -I/home/dev/casablanca/Release/include -lcpprest -std=c++11

编译得很好,但是当我运行./a.out时,我得到了典型的运行时错误:

couldn't load shared library: libcpprest.so

即使将libcpprest.so的默认路径添加到LD_LIBRARY_PATH。

但是,如果我链接最初创建二进制文件的目录,那么一切正常:

// ./a.out runs just fine
g++ index.cpp -I/home/dev/casablanca/Release/include \
   -L/home/dev/casablanca/Release/build.release/Binaries -lcpprest -std=c++11

我猜我之所以不能简单地将.so对象移动到我想要添加它的位置,原因是编译器以某种方式保留对它的引用。如何在不同的路径中安装此二进制文件?

2 个答案:

答案 0 :(得分:1)

您所指的部分由rpath开关调整:

g ++ -Wl,-rpath,/ path / to / lib ...

答案 1 :(得分:1)

我用我的linux debian(https://git01.codeplex.com/casablanca)编写了卡萨布兰卡程序https://casablanca.codeplex.com/wikipage?title=Setup%20and%20Build%20on%20Linux&referringTitle=Documentation

编译后我得到一个libcpprest.so(objdump):

SONAME libcpprest.so.2.2

所以你可能想把libcpprest.so.2.2复制到/usr/lib/libcpprest.so.2.2

或使用ldconfig工具执行此操作。

查看Release / build.release / Binaries你会发现:

libcpprest.so -> libcpprest.so.2.2
libcpprest.so.2.2

然后 libcpprest.so只是一个链接,真正的库是libcpprest.so.2.2