2015-12-09 120 views
3

我發現這個代碼在教程編譯C++鏗鏘 - 新手

http://www.penguinprogrammer.co.uk/c-beginners-tutorial/introduction/

// This line is necessary to be able to output information to the screen 
#include <iostream> 

// The program starts here and carries on line by line 
int main(){ 
    // Create two integers a and b containing 10 and 5 
    int a = 10; 
    int b = 5; 

    /* Add them together and store the result in another 
     integer called sum */ 
    int sum = a + b; 

    // Output the sum to the screen 
    std::cout << sum << std::endl; 

    // End the program and send a value of 0 (success) back 
    // to the operating system 
    return 0; 
} 

我想編譯

有做

apt-get install clang 

編譯安裝clang通過做

clang -x c++ tutorial.cpp 

錯誤

/tmp/tutorial-aa5f7a.o: In function `main': 
tutorial.cpp:(.text+0xa): undefined reference to `std::cout' 
tutorial.cpp:(.text+0x34): undefined reference to `std::ostream::operator<<(int)' 
tutorial.cpp:(.text+0x3a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' 
tutorial.cpp:(.text+0x46): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))' 
/tmp/tutorial-aa5f7a.o: In function `__cxx_global_var_init': 
tutorial.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()' 
tutorial.cpp:(.text.startup+0x19): undefined reference to `std::ios_base::Init::~Init()' 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

回答

2

使用clang++ tutorial.cpp - 的-x c++是有用的,如果你只是想編譯源文件,使用-c,但如果你還應用連接成一個可執行文件,你想clang知道您正在鏈接一個C++應用程序,並將C++庫添加到鏈接命令中(如果您想查看clang實際執行的操作,請添加-v選項。