84

我写这段代码来读取 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
4

3 回答 3

169

您可以通过多种方式解决此问题:

  • 使用g++代替gccg++ -g -o MatSim MatSim.cpp
  • 添加-lstdc++gcc -g -o MatSim MatSim.cpp -lstdc++
  • 替换<string.h><string>

这是链接器问题,而不是编译器问题。问题iostream linker error涵盖了同样的问题——它解释了发生了什么。

于 2012-06-06T01:46:42.050 回答
7

大多数这些链接器错误是由于缺少库而发生的。

我在我的 Project->Targets->Build Phases-> Link Binary With Libraries 中添加了libstdc++.6.dylib 。

这在 Xcode 6.3.2 for iOS 8.3 上为我解决了这个问题

干杯!

于 2015-06-30T14:57:09.707 回答
0

g++ 等价于 gcc -xc++ -lstdc++ -shared-libgcc

我在使用 sizeof() 方法时遇到了类似的错误。通过在 gcc 中使用 g++ 或更高版本的标签,可以编译代码。

于 2021-05-14T12:07:01.707 回答