包含String时编译失败

时间:2017-04-11 03:38:11

标签: c++ string

这是一个简单的C ++程序,我相信它应该可行。我的代码是从这里引用的。 Convert command line argument to string

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector> 
#include <iostream>


//using namespace std;


int main(int argc, char *argv[]){

std::string boss;
//printf("PROGRAM NAME: %s\n", argv[0]);


if (argc > 1){
    //printf("%s\n",&argv[1][0]);
    printf("%s\n",argv[1]);
    boss = argv[1];
}

}

编译时出现问题:

/tmp/cctYLBpB.o:在函数main': testarg.cpp:(.text+0x1c): undefined reference to std :: basic_string中,std :: allocator&gt; :: basic_string()' testarg.cpp :(。text + 0x58):未定义引用std::string::operator=(char const*)' testarg.cpp:(.text+0x64): undefined reference to std :: basic_string,std :: allocator&gt; :: ~basic_string()' testarg.cpp :(。text + 0x78):对std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' testarg.cpp:(.text+0x7c): undefined reference to __ cxa_end_cleanup'的未定义引用 /tmp/cctYLBpB.o:在函数__static_initialization_and_destruction_0(int, int)': testarg.cpp:(.text+0xc0): undefined reference to中std :: ios_base :: Init :: Init()' testarg.cpp :(。text + 0xe4):对std::ios_base::Init::~Init()' /tmp/cctYLBpB.o:(.ARM.extab+0x0): undefined reference to __ gxx_personality_v0'的未定义引用 collect2:错误:ld返回1退出状态

你们这位专家有没有看到任何问题?我不知道......

1 个答案:

答案 0 :(得分:3)

使用gcc构建程序时,链接器不使用标准C ++库。请改用g++。添加-Wall以获得良好衡量标准。您将获得有用的警告消息,这些消息将帮助您在编译时发现问题,而不是在运行时找到它们。

g++ filename.cpp -Wall -o testc