我刚开始学习c ++。当我使用GCC版本编译以下代码时: gcc(Ubuntu 5.2.1-22ubuntu2)5.2.1 20151010 我得到这个错误:
gcc tst.cpp -o mytst
/tmp/ccGA15Qf.o: In function `main':
tst.cpp:(.text+0xa): undefined reference to `std::cout'
tst.cpp:(.text+0xf): 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*)'
tst.cpp:(.text+0x19): undefined reference to `std::cout'
tst.cpp:(.text+0x1e): 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*)'
/tmp/ccGA15Qf.o: In function `__static_initialization_and_destruction_0(int, int)':
tst.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
tst.cpp:(.text+0x5b): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
这是我的代码:
#include <iostream>
int main ()
{
std::cout << "Hello World! ";
std::cout << "I'm a C++ program";
}
出了什么问题?
答案 0 :(得分:3)
你应该使用g ++而不是gcc来编译c ++文件
答案 1 :(得分:0)
&#39; g ++ test.cpp -o test&#39;解决了这个问题