Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when compiling some simple c++ code

I try to compile this cpp code on osx lion but I get an error.

#include <iostream> 

using namespace std; 

int main (int argc, char *argv[]) 
{ 
    for(int i = 0; i < 10; i++) 
    { 
        cout << "hi"; 
        cout << endl; 
    } 

    return 0; 
}

To compile:

cc main.cpp

Error:

Undefined symbols for architecture x86_64:
  "std::cout", referenced from:
      _main in ccBdbc76.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccBdbc76.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in ccBdbc76.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      _main in ccBdbc76.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccBdbc76.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccBdbc76.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
like image 951
Jean-Philippe Leclerc Avatar asked Sep 23 '11 18:09

Jean-Philippe Leclerc


People also ask

What are the compilation errors in C?

There are 5 different types of errors in C programming language: Syntax error, Run Time error, Logical error, Semantic error, and Linker error. Syntax errors, linker errors, and semantic errors can be identified by the compiler during compilation.

What are the 3 types of error in programming?

When developing programs there are three types of error that can occur: syntax errors. logic errors. runtime errors.

What is an error in C language?

Errors are the problems or the faults that occur in the program, which makes the behavior of the program abnormal, and experienced developers can also make these faults. Programming errors are also known as the bugs or faults, and the process of removing these bugs is known as debugging.


3 Answers

Normally this sort of failure happens when compiling your C++ code by invoking the C front-end. The gcc you execute understands and compiles the file as C++, but doesn't link it with the C++ libraries. Example:

$ gcc example.cpp  Undefined symbols for architecture x86_64:   "std::cout", referenced from:       _main in ccLTUBHJ.o   "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:       _main in ccLTUBHJ.o   "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:       _main in ccLTUBHJ.o   "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:       _main in ccLTUBHJ.o   "std::ios_base::Init::Init()", referenced from:       __static_initialization_and_destruction_0(int, int)in ccLTUBHJ.o   "std::ios_base::Init::~Init()", referenced from:       ___tcf_0 in ccLTUBHJ.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status $ g++ example.cpp  $  

As you can see, using g++ makes the problems go away. The same behaviour (with slightly different messages) occurs if you use clang (which I'd recommend):

$ clang example.cpp  Undefined symbols for architecture x86_64:   "std::ios_base::Init::~Init()", referenced from:       ___cxx_global_var_init in cc-IeV9O1.o   "std::ios_base::Init::Init()", referenced from:       ___cxx_global_var_init in cc-IeV9O1.o   "std::cout", referenced from:       _main in cc-IeV9O1.o   "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:       _main in cc-IeV9O1.o   "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:       _main in cc-IeV9O1.o   "std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:       _main in cc-IeV9O1.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) $ clang++ example.cpp  $ 

As you can see in the clang error message, you could use -v to see the linker invocation to see what's going wrong. It would show you this link line:

"/usr/bin/ld" -demangle -dynamic -arch x86_64      -macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o     /var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-hdOL8Z.o     -lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a 

Or something like it - as you can see, it's linking the C runtime, not C++, and also doesn't have the C++ libraries. Using clang++ the link line is:

"/usr/bin/ld" -demangle -dynamic -arch x86_64      -macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o       /var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-wJwxjP.o       /usr/lib/libstdc++.6.dylib -lSystem      /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a 

As you can see, libstdc++ is included, and presto - no link errors.

like image 162
Carl Norum Avatar answered Sep 25 '22 08:09

Carl Norum


Try

g++ main.cpp

This way it should work, at least using OS X

like image 30
Allisone Avatar answered Sep 22 '22 08:09

Allisone


I'm not familiar with OSX LION. However, in the strictest sense, the errors described are not caused by the compiler, but by the linker. It seems as if the standard library is not being linked.

like image 24
S.C. Madsen Avatar answered Sep 23 '22 08:09

S.C. Madsen