对‘ std: : ios_base: : Init: : Init()’的未定义引用

我写这个代码读3个文件,TM 是方阵的大小,LER 是号码。从最后一个值定义一个非平方矩阵(ler/2) * 2

然后... 代码读取一个具有某些关系的文件,所有这些关系都是数字,并分配给 C [ ler ]。

然后... C [ ler ]被分配给 B [ ler/2][2]。

B [ ler/2][2]中每行的坐标分配给 a 和 b。

A 和 b 是矩阵 A [ tm ][ tm ]的行和列,其中要加1。

我的代码崩溃了,我不知道是什么错误。

当我尝试编译它时,编译器 Gcc-g-o MatSim MatSim.cpp提示:

/usr/include/c++/4.6/iostream:75: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.6/iostream:75: undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status

另外,当我尝试编译它时,编译器 F77-o MatSim MatSim.cpp提示:

/tmp/cc6ewlkf.o: In function `__static_initialization_and_destruction_0(int, int)':
MatSim.cpp:(.text+0x17ad4a): undefined reference to `std::ios_base::Init::Init()'
MatSim.cpp:(.text+0x17ad4f): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status

解决方案

主要问题是库问题,使用以下方法编译代码:

 g++ -g -o MatSim MatSim.cpp -lstdc

还是不能工作? 安装库:

sudo apt-get install g++-multilib
149758 次浏览

You can resolve this in several ways:

  • Use g++ in stead of gcc: g++ -g -o MatSim MatSim.cpp
  • Add -lstdc++: gcc -g -o MatSim MatSim.cpp -lstdc++
  • Replace <string.h> by <string>

This is a linker problem, not a compiler issue. The same problem is covered in the question iostream linker error – it explains what is going on.

Most of these linker errors occur because of missing libraries.

I added the libstdc++.6.dylib in my Project->Targets->Build Phases-> Link Binary With Libraries.

That solved it for me on Xcode 6.3.2 for iOS 8.3

Cheers!

g++ is equivalent to gcc -xc++ -lstdc++ -shared-libgcc

I was getting a similar error while using sizeof() method. By using g++ or above tags with gcc, the code got compiled.