linux -/usr/bin/ld : attempted static link of dynamic object `/usr/lib64/libm.so'

标签 linux gcc build redhat

我根本没有使用 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/

相关文章:

linux - 如何将参数传递给作业并以并行方式保持不变

linux - 如何根据 its/file1(仅)第一列与 file2 在 linux 中的匹配信息从 file1 中提取行?

c - 有选择地抑制 glibc 链接警告?

android - 选项 'android.enableR8' 已弃用,不应再使用。它将在未来版本的 Android Gradle 中被删除

java - 在 Linux 上构建 JSCIPOpt(Scip 的 Java 接口(interface))

php - 用特殊字符替换字符串

c++ - GCC 将私有(private)继承转换为父级

gcc - 如何构建GCC编译器的Libgcc

twitter-bootstrap - 在 Ember.JS ember-cli app 中使用 Bootstrap 的 LESS 源的推荐方式

asp.net-mvc - Visual Studio : How to override the default "Build Action" for certain extension types per project or solution?