我在使用 gcc 对c++ 文件进行编译的时候 (命令是 "gcc HelloWorld -o HelloWorld" )报出了错误:
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::s
ize() const'
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::
operator[](unsigned int) const'
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::
operator[](unsigned int) const'
undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::
operator[](unsigned int) const'
undefined reference to `std::cout'
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*)'
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
undefined reference to 'std::basic_string
最新推荐文章于 2024-05-23 16:24:13 发布
在使用GCC编译C++文件时,若出现未定义引用到std::basic_string相关函数的问题,可通过添加-lstdc++参数进行解决。此外,g++编译器会自动连接stdc++库。
8052

被折叠的 条评论
为什么被折叠?



