Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

gcc 4.8 on MAC OS X 10.8 throws "Undefined symbols for architecture x86_64: "

all,I write a code like this in my mac os x 10.8,and when I use "gcc use_new.cpp -o use_new " to compile it,but it throws wrong message like this:

Undefined symbols for architecture x86_64:
  "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 ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(void const*)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(double)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
      _main in ccr2vrRQ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(unsigned long)", referenced from:
      _main in ccr2vrRQ.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccr2vrRQ.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccr2vrRQ.o
  "std::cout", referenced from:
      _main in ccr2vrRQ.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 ccr2vrRQ.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 ccr2vrRQ.o
  "operator delete(void*)", referenced from:
      _main in ccr2vrRQ.o
  "operator new(unsigned long)", referenced from:
      _main in ccr2vrRQ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

and when I use "g++ use_new.cpp -o use_new" is OK,who can help me!? thank you!

#include <iostream>      
    struct fish              
    {
        float weight;
        int id;
        int kind;              
    };
    int main()               
    {
      using namespace std;   
      int* pt = new int;     
      *pt = 1001;            
      cout<<"int: "<<*pt<<"in location: "<<pt<<endl;
      double* pd = new double;
      *pd = 100000001.0;     
      cout<<"double: "<<*pd<<"in location: "<<pd<<endl;
      cout<<"int point pt is length "<<sizeof(*pt)<<endl;
      cout<<"double point pd is length "<<sizeof(*pd)<<endl;
      delete pt;             
      delete pd;             
      cout<<(int *)"How are you!"<<endl;
      return 0;
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the case even with the old 4.2 GCC (I experienced this when I set up my unofficial iOS toolchain). gcc assumes C by default, and invokes the linker without linking to the C++ standard library; in contrast, g++ assumes C++ and links against the C++ standard library by default.

All in all - possible solutions:

gcc myprog.c -o myprog -lstdc++

or

g++ myprog.c -o myprog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...