1

okay this I did accidentally , compiling .cpp using gcc and not g++

but I actually want to understand the console output, line by line, if it has any sense.

struct a{
   int pointer;
   int  rollno;
};

struct a student,*studentref;
studentref = &student;
studentref->rollno = 141; 
studentref->pointer = 8;

cout<<studentref->rollno<<") : "<<studentref->pointer<<endl;

compiling this code with gcc structpointers.cpp -o structp gives the following output:

sourab@sourab:/home/gbear/coding/learningds$ gcc structpointers.cpp -o structp
/tmp/ccXrq1Cv.o: In function `main':
structpointers.cpp:(.text+0x2e): undefined reference to `std::cout'
structpointers.cpp:(.text+0x33): undefined reference to `std::ostream::operator<<(int)'
structpointers.cpp:(.text+0x38): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
structpointers.cpp:(.text+0x40): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccXrq1Cv.o: In function `__static_initialization_and_destruction_0(int, int)':
structpointers.cpp:(.text+0x6e): undefined reference to `std::ios_base::Init::Init()'
structpointers.cpp:(.text+0x7d): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
1
  • Your question should be "linking c++ program with gcc", compiling .cpp by gcc pretty fine - gcc determines mode by file extension. Commented Apr 26, 2017 at 11:42

2 Answers 2

2

The most egregious difference between calling g++ and gcc on a .cpp file is that g++ automatically links in the C++ standard library, while gcc does not; all the errors you see are linker errors of missing references to stuff that is provided by the C++ standard library.

(notice that this isn't the only difference; see this question for details)

Sign up to request clarification or add additional context in comments.

Comments

0

g++ and gcc are compiler drivers of GNU compiler collection, they have backends which are connected automatically once compilation mode in up. The errors like "

structpointers.cpp:(.text+0x2e): undefined reference to `std::cout'

are link errors,since you have used gcc to compile cpp,the standard libraries specifically for cpp are not present, so it could not find syntax like std::cout etc.

you can also refer to : compiling with g++

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.