如何通过 g++ 编译独立的可执行文件

How to compile an independent executable file by g++?

本文关键字:可执行文件 独立 编译 何通过 g++      更新时间:2023-10-16

我在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

问候乌尔夫

您需要

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

例如,如果要在系统上的/usr/local/boost-1.56.0/lib64中找到libboost_system.so文件(或符号链接),则应运行该命令。

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

要准确找出所需的库,可以运行以下命令:

ldd my_binary

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

相关文章: