使用Clang编译C++ - 新手

3
我在教程中找到了这段代码。 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个回答

2
使用clang++ tutorial.cpp命令来编译源代码文件,如果你只需要编译源文件,则使用-x c++选项非常有用。但是,如果你还要将应用程序链接到可执行文件中,你需要让clang知道你正在链接一个C++应用程序,并将C++库添加到链接命令中(如果你想查看clang的实际操作,请添加-v选项)。请注意保留HTML标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接