【问题标题】:Multiple definition error in static compilation with g++ under cygwincygwin下用g++静态编译多定义错误
【发布时间】:2018-04-01 22:27:49
【问题描述】:

我正在尝试在 cygwin 下使用 g++ 编译一个大型代码库。编译失败,我已将问题简化为以下独立测试用例:

文件 main.cpp:

#include <stdexcept>
int main() {
  std::logic_error One("One");
  std::logic_error Two(One);
  return 0;
}

编译命令及结果:

$ g++ -std=c++11 -static main.cpp
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/libstdc++.a(cow-stdexcept.o): In function `std::logic_error::logic_error(std::logic_error const&)':
/usr/src/debug/gcc-6.4.0-1/libstdc++-v3/src/c++11/cow-stdexcept.cc:59: multiple definition of `std::logic_error::logic_error(std::logic_error const&)'
/tmp/ccCSKFES.o:main.cpp:(.text$_ZNSt11logic_errorC1ERKS_[_ZNSt11logic_errorC1ERKS_]+0x0): first defined here
collect2: error: ld returned 1 exit status

该文件在 CentOS 上使用 g++ 5.3.1 和 6.3.1 编译和链接。它无法在 cygwin 下与 g++ 6.3.0 和 6.4.0 链接。如果我省略标志“-static”,它总是编译和链接。 这是cygwin下g ++中的错误,还是我的代码有问题?

【问题讨论】:

    标签: c++11 g++ cygwin


    【解决方案1】:

    你可以试试

    g++  -Wl,--allow-multiple-definition -static -Wall main.cpp
    

    但如果我没记错的话,C++ 异常在 cygwin 上的静态构建中不能很好地工作。

    【讨论】:

      猜你喜欢
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      相关资源
      最近更新 更多