undefined reference to `std::allocator::~allocator()'

我在使用 gcc 对c++ 文件进行编译的时候 (命令是 "gcc HelloWorld -o HelloWorld" )报出了错误:

undefined reference to `std::basic_string, std::allocator >::s
ize() const'
undefined reference to `std::basic_string, std::allocator >::
operator[](unsigned int) const'
undefined reference to `std::basic_string, std::allocator >::
operator[](unsigned int) const'
undefined reference to `std::basic_string, std::allocator >::
operator[](unsigned int) const'
undefined reference to `std::cout'
undefined reference to `std::basic_ostream >& std::operator<< ::char_traits >(std::basic_ostream >&, char const*)'
undefined reference to `std::ios_base::Init::Init()'
undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status


解决方法是用如下命令进行编译(使用 -l 来连接 stdc++):

gcc HelloWorld.cpp -lstdc++ -o HelloWorld

另外,你也可以使用 g++, 效果是一样的, stdc++会被自动连接:

g++ HelloWorld.cpp -o HelloWorld

你可能感兴趣的:(C++)