C++编译器命令 C++ 不会生成输出



我已经在文本编辑器(UNIX命令行(中编写了一个C++文件nano我正在尝试使用以下命令执行它:c++ main.cpp。当我运行它时,没有显示错误消息,但是,也没有成功消息;根本什么都没有出现...我也尝试了clang它给了我更多详细信息,单击此处查看错误消息。 我的代码:

#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
return 0;
}

叮当输出:

/usr/bin/ld: /tmp/main-ad7332.o: in function `main':
main.cpp:(.text+0x11): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text+0x24): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: main.cpp:(.text+0x2d): 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> >&)'
/usr/bin/ld: main.cpp:(.text+0x36): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/main-ad7332.o: in function `__cxx_global_var_init':
main.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.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)

没有任何输出是预期的结果。运行c++编译提供的源文件。要运行程序,您必须显式运行生成的可执行文件,默认情况下为a.out.

关于您从收到的错误消息clang,确保您使用的是clang++并记住包含声明std::coutiostream标头。

最新更新