110

I write this code to read 3 files, TM is the size of square matrix, LER the No. of rows of an array and from last value define a non-square matrix of (ler/2)*2

Then... the code read a file with some relations, all are numbers and are assign to C[ler].

Then ... C[ler] is assigned to B[ler/2][2].

Those coordinates, per row, in B[ler/2][2] are assign to a and b.

a and b are the row and the column of the matrix A[tm][tm] where to add 1.

My code crashes and I don't see what the error is.

When I try to compile it, the compiler gcc -g -o MatSim MatSim.cpp prompted:

/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

Also, when I try to compile it, the compiler f77 -o MatSim MatSim.cpp prompted:

/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

Solution

The main problem was a library problem, Compile your code using:

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

Still not working? Install libraries:

sudo apt-get install g++-multilib
5
  • Does the compiler give an error? Are you sure you are correct when saying "the compiler gave an exit status of 1" and not the program gave an exit status of 1? Commented Jun 5, 2012 at 23:24
  • Celeritas, yes, this is what I receive right after I execute any of the compilers [gcc o f77] Commented Jun 5, 2012 at 23:27
  • possible duplicate of Can't find c++ libraries on unix Commented Jun 6, 2012 at 1:51
  • Thanks Jonathan Leffler!!!! you and Reinier have enlightment my code and now I can really go forward!!!! Commented Jun 6, 2012 at 2:48
  • 1
    I had a similar issue using clang, it turns out adding the -lstdc++ switch works for this as well. Commented Dec 20, 2023 at 20:04

3 Answers 3

221

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Reiner... I tried your suggestion and I get: g++ -g -o MatSim MatSim.cpp -lstdc /usr/bin/ld: cannot find -lstdc collect2: ld returned 1 exit status
If you go with the extra library option, then you should use -lstdc++, not -lstdc. But if you use g++, then it is not necessary to add that library.
Thanks Reinier!!!! you and Jonathan Leffler have enlightment my code and now I can really go forward!!!!
I think this is an human error: it is not right to compile c++ code with gcc (instead of g++) (as I was doing wrong myself and googled here :)
I tried to ndk-build to jni directory. I think your answer for one .cpp not for the directory. Do you have solution?
7

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!

1 Comment

Thanks for sharing this, man. You just solved my problem. I was trying to compiling a target for unity test in my project using gtest-1.7.0 Framework and Xcode Version 6.3.2.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.