我根本没有使用 gcc 进行构建的经验,现在需要一些帮助。 我有一个正在使用以下选项构建的代码
gcc \
-g myCode.C \
-O \
-o myCode \
-I. \
-L. \
-L/usr/lib64 \
-lstdc++ \
-Wreturn-type \
-Wswitch \
-Wcomment \
-Wformat \
-Wchar-subscripts \
-Wparentheses \
-Wpointer-arith \
-Wcast-qual \
-Woverloaded-virtual \
-Wno-write-strings /usr/lib64/libm.so \
-Wno-deprecated
在 redhat 6 机器上编译 myCode.C
时,它无法在旧版本的操作系统上运行并抛出错误
/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found
为了解决这个问题,我尝试添加 -static
构建选项以使所有动态链接库成为静态,但有一些我不明白的构建错误:(
/usr/bin/ld: attempted static link of dynamic object `/usr/lib64/libm.so'
collect2: ld returned 1 exit status
如何使我的代码在旧版本的 redhat 上运行而不是仅在 6 及更高版本上运行?我应该添加/删除哪些构建选项?
最佳答案
/usr/lib64/libm.so
是一个动态库。由于您显式链接它,-static
不会强制使用静态版本 (libm.a
)
您正在尝试编译 C++ 程序,因此应该使用 g++。则不需要传递 libstdc++
和 libm
库。另外 /usr/lib64
应该位于您的标准链接路径中,因此不需要。
所以你应该使用:
g++ \
-static \
-g myCode.C \
-O \
-o myCode \
-I. \
-L. \
-Wreturn-type \
-Wswitch \
-Wcomment \
-Wformat \
-Wchar-subscripts \
-Wparentheses \
-Wpointer-arith \
-Wcast-qual \
-Woverloaded-virtual \
-Wno-write-strings \
-Wno-deprecated
关于linux -/usr/bin/ld : attempted static link of dynamic object `/usr/lib64/libm.so' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39827753/