用clang编译c ++ - 新手

时间:2015-12-09 23:57:08

标签: 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;
}

我想编译它

通过执行

安装了clang
apt-get install 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)

1 个答案:

答案 0 :(得分:2)

使用clang++ tutorial.cpp - 如果您只想使用-x c++编译源文件,则-c非常有用,但如果您还要将应用程序链接到可执行文件,则需要clang {1}}要知道您正在链接C ++应用程序,并将c ++库添加到link命令(如果您想查看clang实际执行的操作,请添加-v选项。

相关问题