-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 ...

4
  • What command(s) did you use to build your program? Commented Apr 11, 2017 at 3:47
  • the command(s) i use to compile is gcc "filename".cpp -o testc Commented Apr 11, 2017 at 3:48
  • 1
    Use g++ to compile/build. Commented Apr 11, 2017 at 3:50
  • wow ! g++ is work , i accidentally forgot gcc is for c program, g++ is for c++ program ! thanks Commented Apr 11, 2017 at 3:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.