2012-08-07 79 views
23

所有,我在我的mac os x 10.8中編寫這樣的代碼,並且當我使用「gcc use_new.cpp -o use_new」編譯它時,但它拋出錯誤信息是這樣的:MAC OS X 10.8上的gcc 4.8拋出「架構x86_64的未定義符號:」

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 

,當我用「G ++ use_new.cpp -o use_new」就行了,誰可以幫我!?謝謝!

#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; 
    } 
+0

我遇到了'i686-apple-darwin11-llvm-gcc-4.2(GCC)4.2.1'類似的問題,試圖編譯一個「Hello world」程序。開始賞金。 – Randomblue 2012-08-15 19:03:51

+2

這已經被問過很多次了(例如[這裏](http://stackoverflow.com/questions/5590983/problem-occurred-while-compiling-a-cpp-program-in-gcc) ,[here](http://stackoverflow.com/questions/1221902/programs-compiles-in-g-but-exits-with-linker-errors-in-gcc?rq=1)或[here](http: //stackoverflow.com/questions/8904722/gcc-linker-cant-find-standard-library))。你會受益於閱讀[g ++和gcc有什麼區別?](http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc)。 – Troubadour 2012-08-18 00:43:31

回答

5

回答這個計算器的問題有答案

gcc and g++ linker

使用

gcc -lstdc++ use_new.cpp -o use_new 

-lstdc++標誌告知鏈接包括C++標準庫

http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library

我運行Mac OS X 10.7.4和圖書館在這裏

/usr/lib/libstdc++.dylib 
+0

謝謝;這樣可行。你能否詳細說明國旗? – Randomblue 2012-08-15 19:17:40

+0

它告訴鏈接器包含C++標準庫,回答編輯以包含該內容。 – amdn 2012-08-15 19:27:37

66

這種情況甚至與老4.2 GCC位於(我經歷過這樣的當我建立了我的非官方的iOS工具鏈)。 gcc默認爲C,並調用鏈接器而不鏈接到C++標準庫;相反,g++默認採用C++並與C++標準庫鏈接。

所有的一切 - 可能的解決方案:

gcc myprog.c -o myprog -lstdc++ 

g++ myprog.c -o myprog 
+0

我明白了。你會推薦使用更新版本的GCC嗎? – Randomblue 2012-08-15 19:29:45

+0

@Randomblue沒有更新的GCC版本 - 甚至4.8版本正在開發中。要麼使用'-lsdtC++'進行鏈接,要麼更好(我建議你這樣做):使用g ++進行鏈接。 – 2012-08-15 19:30:57

+0

謝謝你的賞金。 – 2012-08-19 21:17:48

3

這不與由@ user1582840只是我的2美分粘貼代碼,並從不同的事業g ++在處理我自己的某些代碼時遇到同樣的問題:

在OS X 10.8/Darwin11上使用g ++ 4.2.1時收到「ld:symbol(s)not found for architecture x86_64」錯誤理由。希望這將有助於一些搜索谷歌的問題。

我收到了這個錯誤,因爲我有一個定義的類,在類定義中聲明瞭成員函數。但是,當我定義成員函數時,我忘記了包含類修飾符。

所以對於什麼我談論(代碼示例,不完整的程序)的例子:後來

class NewClass 
{ 
    NewClass(); // default constructor 
}; 

然後,定義的NewClass()構造函數(或任何成員函數)時,我根本就:

// don't do this, it will throw that error!! 
NewClass() 
{ 
    // do whatever 
} 

而不是:

// proper way 
NewClass::NewClass() 
{ 
    // do whatever 
} 

這是一個相當簡單的錯誤,而我管理的牛逼o幸運地在短時間內抓住它,但可能很容易讓某人錯過(特別是我們的新手),以及有關gcc/g ++連接器,XCode等的解決方案。對此沒有任何幫助:P

再次,希望它有幫助!

相關問題