2015-12-09 123 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选项。