如何仅静态链接 Boost 库?

发布于 2024-11-04 19:38:26 字数 1131 浏览 0 评论 0原文

我有一个项目链接到各种公共库以及 Boost。事实证明,在我自己的计算机以外的计算机上进行测试很困难,因为不同版本的 Linux 都带有不同版本的 Boost。我希望避免在每台机器上下载并编译相同版本的 Boost。

有没有一种方法可以将我的程序仅与 Boost 库静态链接,而使其他所有内容正常链接?我尝试过静态链接所有内容(-static),但这会导致其他问题(即找不到 lGL)。是否有其他潜在的方法可以仅将必要的动态库与我的程序打包(例如与可执行文件在同一文件夹中)并以这种方式分发?

尝试静态链接所有内容时出现链接错误:

g++ -static -o"acmserver"  ./src/acmserver.o ./src/airplane.o ./src/bullet.o ./src/control.o ./src/detail.o ./src/game.o ./src/gamelog.o ./src/gamelogitem.o ./src/guns.o ./src/map.o ./src/missile.o ./src/missilepod.o ./src/object.o ./src/server.o   -lboost_system -lboost_filesystem -lboost_thread -lboost_serialization -lboost_date_time -lpthread -lGLU -lGL
/usr/bin/ld: cannot find -lGL
collect2: ld returned 1 exit status
make: *** [acmserver] Error 1

编辑(解决方案):

count0 准确地提到了我正在寻找的内容。在 Eclipse 中,我从 Project -> 中删除了所有 Boost 库(例如 boost_system)。属性-> C/C++ 构建 ->设置-> GCC C++ 链接器 ->图书馆 ->库(-l)。然后我在 Project -> 下添加了 Boost .a 文件(例如 /usr/lib/libboost_system.a)属性-> C/C++ 构建 ->设置-> GCC C++ 链接器 ->杂项 ->其他对象。我还从链接器标志中删除了“-static”。这生成了一个可执行文件,其中所有 boost 库都是静态链接的,而不是动态链接的。

I've got a project which links against various common libraries, as well as Boost. Testing this on computers other than my own has proven to be difficult since various flavors of Linux come with different versions of Boost. I would prefer to avoid having to download and compile the same version of Boost on every machine.

Is there a way to link my program statically only with the Boost libraries and have everything else linked normally? I've tried linking everything statically (-static), but that causes other problems (namely lGL is not found). Is there an other potential way I could package only the necessary dynamic libraries with my program (say in the same folder as the executable) and distribute it that way?

Link error when trying to link everything statically:

g++ -static -o"acmserver"  ./src/acmserver.o ./src/airplane.o ./src/bullet.o ./src/control.o ./src/detail.o ./src/game.o ./src/gamelog.o ./src/gamelogitem.o ./src/guns.o ./src/map.o ./src/missile.o ./src/missilepod.o ./src/object.o ./src/server.o   -lboost_system -lboost_filesystem -lboost_thread -lboost_serialization -lboost_date_time -lpthread -lGLU -lGL
/usr/bin/ld: cannot find -lGL
collect2: ld returned 1 exit status
make: *** [acmserver] Error 1

EDIT (SOLUTION):

count0 mentioned exactly what I was looking for. In Eclipse I removed all the Boost libraries (e.g., boost_system) form Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries -> Libraries (-l). Then I added the Boost .a files (e.g., /usr/lib/libboost_system.a) under Project -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Miscellaneous -> Other Objects. I also removed the "-static" from the linker flags. This produced an executable with all of the boost libraries linked statically instead of dynamically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-11-11 19:38:26

使用 boost 存档文件(.a 文件)而不是共享 lib 文件(.so 又名,用 -l 链接)。您现在正在动态链接这些 boost 库。将其写出来可能有助于确保哪些内容是静态链接的,哪些是动态链接的。

这看起来像:

g++ -o"acmserver"  ./src/acmserver.o ... ./src/server.o \
  /usr/local/lib/libboost_system.a /usr/local/lib/boost_filesystem \
  ... -lGL ...

根据 gcc 版本或平台类型,您可能还需要添加 -static` 标志。

Use the boost archive files (.a files) instead of the shared lib files (.so aka. linking with -l). You are linking those boost libraries dynamically right now. Writing it out might help to ensure what is being linked statically and what dynamically.

This will look something like:

g++ -o"acmserver"  ./src/acmserver.o ... ./src/server.o \
  /usr/local/lib/libboost_system.a /usr/local/lib/boost_filesystem \
  ... -lGL ...

Depending on the gcc version or platform type you might also have to add the -static` flag.

破晓 2024-11-11 19:38:26

该错误表明您的链接器没有找到 GL 库,这与 boost 无关。

确保您的系统上安装了 libgl1-mesa-dev 或等效的软件包。

That error says your linker didn't found the GL library, which has nothing to do with boost.

Make sure you have libgl1-mesa-dev or an equivalent package installed on your system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文