简体   繁体   English

g++ 提升 iostreams zlib 链接

[英]g++ boost iostreams zlib linking

I compiled boost iostreams with zlib and bzip2 support according to this tutorial https://www.boost.org/doc/libs/1_49_0/libs/iostreams/doc/installation.html :我根据本教程https://www.boost.org/doc/libs/1_49_0/libs/iostreams/doc/installation.html编译了支持zlibbzip2boost iostreams

I changed working directory to ~/cpp_libs/boost_code/boost_1_55_0/libs/iostreams/build/ and typed:我将工作目录更改为~/cpp_libs/boost_code/boost_1_55_0/libs/iostreams/build/并输入:

bjam -s ZLIB_SOURCE=~/cpp_libs/zlib_code/zlib-1.2.11 -s BZIP2_Source=~/cpp_libs/bzip2_code/bzip2

It has generated libs:它生成了库:

~/cpp_libs/boost_code/boost_1_55_0/bin.v2/standalone/zlib/gcc-9/debug/libboost_zlib.so.1.55.0 ~/cpp_libs/boost_code/boost_1_55_0/bin.v2/libs/iostreams/build/bzip2/gcc-9/debug/libboost_bzip2.so.1.55.0 ~/cpp_libs/boost_code/boost_1_55_0/bin.v2/libs/iostreams/build/gcc-9/debug/libboost_iostreams.so.1.55.0 ~/cpp_libs/boost_code/boost_1_55_0/bin.v2/standalone/zlib/gcc-9/debug/libboost_zlib.so.1.55.0 ~/cpp_libs/boost_code/boost_1_55_0/bin.v2/libs/iostreams/build/bzip2/gcc -9/debug/libboost_bzip2.so.1.55.0 ~/cpp_libs/boost_code/boost_1_55_0/bin.v2/libs/iostreams/build/gcc-9/debug/libboost_iostreams.so.1.55.0

However now I don't know how to link to this libraries, because it has version extensions in their names.但是现在我不知道如何链接到这个库,因为它的名称中有版本扩展。

If I rename libraries:如果我重命名库:

libboost_zlib.so.1.55.0 to libboost_zlib.so libboost_zlib.so.1.55.0 到 libboost_zlib.so

libboost_bzip2.so.1.55.0 to libboost_bzip2.so libboost_bzip2.so.1.55.0 到 libboost_bzip2.so

libboost_iostreams.so.1.55.0 to libboost_iostreams.so libboost_iostreams.so.1.55.0 到 libboost_iostreams.so

I can link them with command " -lboost_zlib -lboost_iostreams -lboost_bzip2 ", however when I run the compiled program it prints:我可以将它们与命令“ -lboost_zlib -lboost_iostreams -lboost_bzip2 ”链接起来,但是当我运行已编译的程序时,它会打印:

./main: error while loading shared libraries: libboost_iostreams.so.1.55.0: connot open shared object file: No such file or directory. ./main:加载共享库时出错:libboost_iostreams.so.1.55.0:无法打开共享 object 文件:没有这样的文件或目录。

So how to link to this libraries without renaming?那么如何在不重命名的情况下链接到这个库呢?

I am using g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0我正在使用 g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

Here's the usual workflow that's used to build and install shared libraries on Linux.这是用于在 Linux 上构建和安装共享库的常用工作流程。 The exact details vary widely between different libraries and all packages but they all follow the same general framework:不同的库和所有包之间的确切细节差异很大,但它们都遵循相同的通用框架:

  1. The software package builds an installation image, placing the libraries as <libdir>/<name>.<version> , where <libdir> is the system library installation directory, like /usr/lib or /usr/lib64 , or perhaps /lib or /lib64 , or there could be several variations.软件 package 构建安装映像,将库放置为<libdir>/<name>.<version> ,其中<libdir>是系统库安装目录,例如/usr/lib/usr/lib64 ,或者可能是/lib/lib64 ,或者可能有几种变体。

  2. <name> is the base name of the shared library, such as libboost_zlib.so in your case, and is the epoch/version/release of the shared library, or 1.55.0 . <name>是共享库的基本名称,例如libboost_zlib.so在您的情况下,是共享库的 epoch/version/release 或1.55.0

  3. As part of building the shared library, the library's name , epoch and version is recorded in the shared library itself, in this case it would be libboost_zlib.so.1.55 .作为构建共享库的一部分,库的nameepochversion记录在共享库本身中,在本例中为libboost_zlib.so.1.55

  4. At this point the actual details start to diverge between different Linux distributions.此时,不同 Linux 分布之间的实际细节开始出现分歧。 In some this step occurs as part of creating an installation package.在某些情况下,此步骤作为创建安装 package 的一部分发生。 On other Linux distributions this happens when the package actual gets installed, but at some point the ldconfig tools gets executed.在其他 Linux 发行版上,当实际安装 package 时会发生这种情况,但有时会执行ldconfig工具。

  5. ldconfig goes through and creates all the needed symbolic links for all shared libraries in standard system shared library directories. ldconfig遍历并为标准系统共享库目录中的所有共享库创建所有需要的符号链接。 In this case it would create the following symbolic links:在这种情况下,它将创建以下符号链接:

libboost_zlib.so => libboost_zlib.so.1.55
libboost_zlib.so.1.55 => libboost_zlib.so.55.0

This ends up being the final state of the installed shared library.这最终成为已安装共享库的最终 state。 When you build the software with the shared library the -lboost_zlib flag to the linker results in the linker attempting to link the executable with libboost_zlib.so .当您使用共享库构建软件时, linker 的-lboost_zlib标志会导致 linker 尝试将可执行文件与libboost_zlib.so链接。 This reads the libboost_zlib.so.1.55 encoded in the actual shared library (see step 3 above), and the linked executable is marked as requiring libboost_zlib.so.1.55 to be loaded when it gets executed.这会读取在实际共享库中编码的libboost_zlib.so.1.55 (参见上面的步骤 3),并且链接的可执行文件被标记为需要在执行时加载libboost_zlib.so.1.55

When the linked executable gets executed, an attempt is made to open libboost_zlib.so.1.55 , which uses that symbolic link to find the actual shared library.当链接的可执行文件被执行时,会尝试打开libboost_zlib.so.1.55 ,它使用该符号链接来查找实际的共享库。

I changed working directory to ~/cpp_libs/boost_code/boost_1_55_0/libs/iostreams/build/ and typed:我将工作目录更改为 ~/cpp_libs/boost_code/boost_1_55_0/libs/iostreams/build/ 并输入:

bjam -s ZLIB_SOURCE=~/cpp_libs/zlib_code/zlib-1.2.11... bjam -s ZLIB_SOURCE=~/cpp_libs/zlib_code/zlib-1.2.11...

You used a non-standard process for compiling and building these shared libraries.您使用了非标准流程来编译和构建这些共享库。 It's now up to you to fill in the missing steps, do all the heavy lifting, and create the missing symbolic links.现在由您来填写缺失的步骤、完成所有繁重的工作并创建缺失的符号链接。 Note that you can comandeer ldconfig into doing your bidding, by pointing it at your installation directory, see its manual page for more information.请注意,您可以通过将ldconfig指向您的安装目录来让 ldconfig 参与您的竞标,请参阅其手册页以获取更多信息。

You will also need to deal with the fact that the shared libraries ended up getting installed in a non-standard directory that the runtime loader will not search by default.您还需要处理这样一个事实,即共享库最终安装在运行时加载程序默认不会搜索的非标准目录中。 You will need to use additional compilation options (namely -rpath ) when linking code with the shared libraries in that directory, but that's going to be a separate issue.将代码与该目录中的共享库链接时,您将需要使用其他编译选项(即-rpath ),但这将是一个单独的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM