-1

Here is a simple C++ program and I believe it should work. My code is reference from here. 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];
}

}

When Compile it come out the problem:

/tmp/cctYLBpB.o: In function main': testarg.cpp:(.text+0x1c): undefined reference tostd::basic_string, std::allocator >::basic_string()' testarg.cpp:(.text+0x58): undefined reference to std::string::operator=(char const*)' testarg.cpp:(.text+0x64): undefined reference tostd::basic_string, std::allocator >::~basic_string()' testarg.cpp:(.text+0x78): undefined reference to 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: In function __static_initialization_and_destruction_0(int, int)': testarg.cpp:(.text+0xc0): undefined reference tostd::ios_base::Init::Init()' testarg.cpp:(.text+0xe4): undefined reference to std::ios_base::Init::~Init()' /tmp/cctYLBpB.o:(.ARM.extab+0x0): undefined reference to__gxx_personality_v0' collect2: error: ld returned 1 exit status

Did you guys expert had seen any problem on it ? I have no idea ...

Community
  • 1
  • 1
Jackdon Chew
  • 117
  • 1
  • 2
  • 10

1 Answers1

3

When you use gcc to build your program, the standard C++ libraries are not used by the linker. Use g++ instead. Add -Wall for good measure. You will get useful warning messages that will help you find problems at compile time rather than find them at run time.

g++ filename.cpp -Wall -o testc
R Sahu
  • 204,454
  • 14
  • 159
  • 270