【问题标题】:How to compile an independent executable file by g++?g++如何编译出独立的可执行文件?
【发布时间】:2016-05-27 09:28:02
【问题描述】:

我在我的 OpenSUSE 上开发了一个 c++ 软件,我喜欢在另一个 OpenSUSE 系统上进行测试。

我在另一个系统上复制了可执行文件,但是当我启动它时,我收到一个错误:

“加载共享库时出错:libboost_system.so.1.61.0: 无法打开共享对象文件:没有这样的文件或目录”

如何编译没有依赖的独立可执行文件?

Eclipse 会这样做:

17:24:41 **** Incremental Build of configuration Debug for project boostServer ****
make all 
Building file: ../src/boostCom.cpp
Invoking: Cross G++ Compiler
g++ -I/opt/boost -I/usr/local/lib -O0 -g3 -Wall -c -fmessage-length=0 -isystem /opt/boost -MMD -MP -MF"src/boostCom.d" -MT"src/boostCom.o" -o "src/boostCom.o" "../src/boostCom.cpp"
Finished building: ../src/boostCom.cpp

Building file: ../src/main.cpp
Invoking: Cross G++ Compiler
g++ -I/opt/boost -I/usr/local/lib -O0 -g3 -Wall -c -fmessage-length=0 -isystem /opt/boost -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp

Building target: boostServer
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "boostServer"  ./src/boostCom.o ./src/boostServer.o ./src/main.o   -lboost_system -lboost_serialization -lboost_thread -lboost_date_time -lpthread
Finished building target: boostServer

问候乌尔夫

【问题讨论】:

  • 你可以使用静态链接。
  • @Ulf 在链接 g++ 中使用 -static。 (即您列表中的最后一个),那么,如果有静态库,它将更喜欢它们共享。

标签: c++ g++ executable libraries


【解决方案1】:

您需要确保您的系统上安装了 boost 库,并且它们的路径包含在您的 LD_LIBRARY_PATH 变量中。

例如,如果libboost_system.so 文件(或符号链接)位于您系统上的/usr/local/boost-1.56.0/lib64,那么您将运行该命令。

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/boost-1.56.0/lib64`

要确切了解您需要哪些库,您可以运行以下命令:

ldd my_binary

输出将准确告诉您需要哪些库,哪些当前无法解析。

【讨论】:

  • 我是否需要在其他系统上始终安装所有使用的库?我不能将它包含在可执行文件中吗?
  • 不,不是全部 - 只是您尝试运行的二进制文件所需的那些。我已经更新了我的答案以展示如何做到这一点
  • 好的,如果我通过 mingw 为 Windows 机器交叉编译,这将如何工作? *.exe 文件应包含所有内容。
  • 哦,天哪...那么您可以在最终构建行上使用 -static 选项,如 SHR 在上面的评论中建议的那样,假设您有可用的静态版本的 boost 和编译器,这将起作用可以找到他们。如果您还没有它们,您可以使用 b2 和 link=static 选项来构建它们。但一般来说,从linux机器交叉编译windows?我真诚地祝你好运。这在技术上是可行的,但在到达那里之前,您可能会发现自己很头疼。
  • 静态适用于 boost 库,但不适用于这些库:找不到 -lpthread -lm 和 -lc
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 2011-03-18
  • 2010-12-20
  • 2012-08-01
  • 2011-05-26
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
相关资源
最近更新 更多