C++ 编译包含字符串的代码时出错

C++ 编译包含字符串的代码时出错,c++,C++,我有一段简单的代码在编译时遇到问题。 #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { string hello = "Hello World"; printf("%s\n",hello.c_str()); return 0; } #包括 #包括 使用名称空间std; int main(int argc,字符**argv

我有一段简单的代码在编译时遇到问题。

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char **argv) {
  string hello = "Hello World";
  printf("%s\n",hello.c_str());
  return 0;
}

#包括
#包括
使用名称空间std;
int main(int argc,字符**argv){
string hello=“hello World”;
printf(“%s\n”,hello.c_str());
返回0;
}
使用“gcc test.cpp-o test”编译 返回以下消息。

/tmp/ccgbRaOf.o:在函数
main'中:
test.cpp:(.text+0x27):对
std::allocator::allocator()的未定义引用 test.cpp:(.text+0x3e):对
std::\uuuuCXX11::basic\u string::basic\u string(char const*,std::allocator const&)的未定义引用
test.cpp:(.text+0x4a):对
std::allocator::~allocator()的未定义引用 test.cpp:(.text+0x56):对
std::\uuucx11::basic\u string::c\u str()常量的未定义引用
test.cpp:(.text+0x6a):对
std::\uuucx11::basic\u字符串的未定义引用,std::allocator>::~basic\u字符串() test.cpp:(.text+0x8f):对
std::allocator::~allocator()的未定义引用
test.cpp:(.text+0xa9):对
std::\uuuuCXX11::basic\u字符串的未定义引用,std::allocator>::~basic\u字符串() /tmp/ccgbRaOf.o:在函数
中\u静态\u初始化\u和\u销毁\u 0(int,int)':
test.cpp:(.text+0xe9):对std::ios_base::Init::Init()的未定义引用
test.cpp:(.text+0xfe):对
std::ios\u base::Init::~Init()的未定义引用
/tmp/ccgbRaOf.o:(.data.rel.local.DW.ref.\uuuuGxx\u personality\u v0[DW.ref.\uuuuGxx\u personality\u v0]+0x0):未定义对
\uuuuGxx\u personality\u v0'的引用 collect2:错误:ld返回了1个退出状态


< > >我做错了什么?< /强>

< p>你用<代码> gcc>代码>编译,这是C,但是你的代码是C++。使用
g++

它不起作用,因为您使用的是C编译器。对于C++,需要一个不同的编译器。尝试使用
g++