在cygwin下使用g ++进行静态编译时出现多个定义错误

时间:2017-10-20 17:30:30

标签: c++11 g++ cygwin

我正在尝试用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

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

1 个答案:

答案 0 :(得分:0)

您可以尝试使用

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

但是如果我记得正确的C ++异常在cygwin上使用静态构建就不能很好地工作。