【问题标题】:/usr/bin/ld: attempted static link of dynamic object `/usr/lib64/libm.so'/usr/bin/ld:尝试静态链接动态对象`/usr/lib64/libm.so'
【发布时间】:2017-02-11 04:05:48
【问题描述】:

我完全没有使用 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 和更新版本上运行?我应该添加/删除哪些构建选项?

【问题讨论】:

  • -Wno-write-strings /usr/lib64/libm.so 中删除/usr/lib64/libm.so 并在必要时添加-lm
  • @ymonad /usr/bin/ld: cannot find -lm

标签: linux gcc build redhat


【解决方案1】:

/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

【讨论】:

  • 编译没有错误,但在旧操作系统上运行时仍然存在同样的问题
  • 当然,我忘记在命令中添加-static...已编辑。
猜你喜欢
  • 2012-04-10
  • 1970-01-01
  • 2021-05-07
  • 2011-01-18
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 2023-01-30
  • 2011-07-16
相关资源
最近更新 更多