编译包含字符串的代码时遇到问题
trouble compiling code that includes string
我有一段简单的代码在编译时遇到问题。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv) {
string hello = "Hello World";
printf("%sn",hello.c_str());
return 0;
}
使用 "GCC 测试.cpp -O 测试"编译 返回以下消息。
/tmp/ccgbRaOf.o: 在函数main':
test.cpp:(.text+0x27): undefined reference to
std::allocator::allocator()' test.cpp:(.text+0x3e):对std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
test.cpp:(.text+0x4a): undefined reference to
std::allocator::~allocator()' 的未定义引用 test.cpp:(.text+0x56):未定义对std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
test.cpp:(.text+0x6a): undefined reference to
std::__cxx11::basic_string、std::分配器>::~basic_string()' test.cpp:(.text+0x8f):未定义对std::allocator<char>::~allocator()'
test.cpp:(.text+0xa9): undefined reference to
std::__cxx11::basic_string、std:::分配器>::~basic_string()' 的引用/tmp/ccgbRaOf.o: In function__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0xe9): undefined reference to
std::ios_base::Init::Init()' test.cpp:(.text+0xfe):未定义对std::ios_base::Init::~Init()'
/tmp/ccgbRaOf.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to
__gxx_personality_v0的引用 collect2:错误:ld 返回 1 个退出状态
我做错了什么?
你用gcc
编译它,这是针对 C 的,但你的代码是C++的。使用g++
.
它不起作用,因为您使用的是 C 编译器。对于C++,您需要一个不同的编译器。尝试使用g++
- 编译包含字符串的代码时遇到问题
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从其他类访问类时遇到问题
- 使用静态成员声明类时遇到问题
- C++ 填充数组时遇到问题
- 在将字符串放入字符串向量时遇到问题?
- 余数除法和不允许除以零 (c++) 时遇到问题
- 我在C++中使用 getline() 函数时遇到问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 在我的四叉树实现中遇到问题
- 在 c++ 中循环遍历数组时遇到问题
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- 编译 Cilk Plus 程序时遇到问题
- 存储另一个函数返回的布尔数组时遇到问题
- 我在 OpenGL 中显示图像时遇到问题
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 在 for 循环中更新两个变量时遇到问题C++
- 为什么我的程序在打开网络设备时遇到问题
- 在 Tensorflow C++ 中使用 FIFOQueue 时遇到问题
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?