2016-10-28 208 views
0
#include <iostream> 
#include <cstdlib> 
#include <ctime>  
using namespace std; 


int main() 
{ 
    srand(time(NULL)); 
    cout << rand() << endl ; 
    return 0; 
} 

錯誤消息:爲什麼這個簡單的C++代碼不能編譯?

Undefined symbols for architecture x86_64: 
    "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
    "std::__1::ios_base::getloc() const", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
    "std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
    "std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
    "std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int)", referenced from: 
     _main in test-c5b4b5.o 
    "std::__1::cout", referenced from: 
     _main in test-c5b4b5.o 
    "std::__1::ctype<char>::id", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
    "std::__1::locale::~locale()", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
    "std::terminate()", referenced from: 
     ___clang_call_terminate in test-c5b4b5.o 
    "___cxa_begin_catch", referenced from: 
     ___clang_call_terminate in test-c5b4b5.o 
    "___gxx_personality_v0", referenced from: 
     std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test-c5b4b5.o 
     Dwarf Exception Unwind Info (__eh_frame) in test-c5b4b5.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
+2

你使用什麼編譯器? – user985030

+1

如果刪除「main」的主體,錯誤是否仍然發生? – Carcigenicate

+1

* hello world *編譯?如果沒有,那麼它看起來像你的編譯器是fubar'd – NathanOliver

回答

4

BLUF:使用g++,不gcc

這是連接器錯誤的示例。您的程序編譯精細—您可以在錯誤消息—中看到對中間*.o文件的引用,但是它遇到了將某些東西鏈接在一起以產生可執行文件的問題。鏈接程序找不到該程序所需的許多功能(例如,某些用於庫文件iostream)。

的原因是因爲你用來構建程序的命令的方式,取得了連接嘗試打造爲ç程序—特別做的,它只是連接對Ç標準庫,而不是C++標準庫,這就是爲什麼它找不到那些東西。

通過將您的構建命令更改爲使鏈接器知道它正在構建C++程序的命令,錯誤消失。

我已經使用GNU編譯器集合—而gcc是足夠聰明的編譯一個程序*.cpp作爲C++代碼,它仍試圖將結果作爲一個C程序鏈接碰上這個自己。因此,您需要使用g++命令來構建C++程序。

相關問題