29

I cut&pasted the below code from a previous question into a file called "avishay.cpp" and then ran

gcc avishay.cpp

only to get the following error messages from the linker. What went wrong, what should I have done?

carl@carl-ubuntu:~/Projects/StackOverflow$ gcc -static avishay.cpp 
/tmp/cccRNW34.o: In function `__static_initialization_and_destruction_0(int, int)':
avishay.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()'
avishay.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cccRNW34.o: In function `A::func()':
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x11): undefined reference to `std::cout'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x16): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x1e): 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> >&)'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x36): undefined reference to `std::cout'
avishay.cpp:(.text._ZN1A4funcEv[A::func()]+0x3b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
/tmp/cccRNW34.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

The C++ code (not my code, I was just trying to run it):

#include <iostream>
using namespace std;

class A
{
private:
   int _dmember;

public:
   void func()
   {
     cout<<"Inside A!! "<<endl;
     cout<<_dmember; // crash when reach here.
   }
};

int main ()

{

    A* a= NULL;

    a->func(); // prints "Inside A!!!" 

    return 1;
}
Community
  • 1
  • 1
Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167

11 Answers11

71

You should use g++, not gcc, to compile C++ programs.

For this particular program, I just typed

make avishay

and let make figure out the rest. Gives your executable a decent name, too, instead of a.out.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 5
    Another +1 (if I could) for the "make" tip. I never knew make could/would do that! – Carl Smotricz Nov 08 '09 at 12:56
  • How does 'make' work here? I tried "make serial-rob.c" and it says "Nothing to be done for.." –  Nov 08 '09 at 13:02
  • 2
    The trick, I think, is to let it use its implicit rules. If it works for you like it did for me, then "make serial-rob" (without the .c) should do it. – Carl Smotricz Nov 08 '09 at 13:15
  • 4
    the reason why `make serial-rob.c` doesn't work, is because that file is already made, it's there already. on the other hand, when make looks for a file named serial-rob, it's not there, but it has an implicit rule for making *.c into *.o and that into just * – SingleNegationElimination Nov 08 '09 at 16:06
12

You probably should use g++ rather than gcc.

jackrabbit
  • 5,525
  • 1
  • 27
  • 38
9

Yes, use g++ to compile. It will automatically add all the references to libstdc++ which are necessary to link the program.

g++ source.cpp -o source

If you omit the -o parameter, the resultant executable will be named a.out. In any case, executable permissions have already been set, so no need to chmod anything.

Also, the code will give you undefined behaviour (and probably a SIGSEGV) as you are dereferencing a NULL pointer and trying to call a member function on an object that doesn't exist, so it most certainly will not print anything. It will probably crash or do some funky dance.

blwy10
  • 4,862
  • 2
  • 24
  • 23
  • Yeah, the code was part of another question (linked) and got its due thrashing there. For just a quick test, I don't mind the program being called a.out, and I knew about -o. My main problem was with the compiler name, thanks! – Carl Smotricz Nov 08 '09 at 13:17
7

Update your apt-get:

$ sudo apt-get update
$ sudo apt-get install g++

Run your program.cpp:

$ g++ program.cpp
$ ./a.out
Brian
  • 14,610
  • 7
  • 35
  • 43
Manish Bhadani
  • 437
  • 6
  • 13
4

Use

g++

space followed by the program name. e.g:

g++ prog.cpp

if the filename was "prog.cpp" in this case. if you want to run the program write:

./prog

so i used

"prog"

because it was my filename.

chris
  • 41
  • 1
4

g++ is the C++ compiler under linux. The code looks right. It is possible that you are missing a library reference which is used as such:

g++ -l{library name here (math fns use "m")} codefile.cpp

monksy
  • 14,156
  • 17
  • 75
  • 124
4

even you can compile your c++ code by gcc Sounds funny ?? Yes it is. try it

$  gcc avishay.cpp -lstdc++

enjoy

Javed
  • 795
  • 2
  • 7
  • 14
2

Use g++. And make sure you have the relevant libraries installed.

  • I paid good money for Ubuntu... oh wait, I didn't! But yeah, it turns out the problem was just in how I called the compiler. – Carl Smotricz Nov 08 '09 at 12:58
1

you can use g++ --std=c++0x example.cpp -o example

MSharq
  • 65
  • 1
  • 3
1

To compile source.cpp, run

g++ source.cpp

This command will compile source.cpp to file a.out in the same directory. To run the compiled file, run

./a.out

If you compile another source file, with g++ source2.cpp, the new compiled file a.out will overwrite the a.out generated with source.cpp

If you want to compile source.cpp to a specific file, say compiledfile, run

g++ source.cpp -o compiledfile

or

g++ -o compiledfile source.cpp

This will create the compiledfile which is the compiled binary file. to run the compiledfile, run

./compiledfile

If g++ is not in your $PATH, replace g++ with /usr/bin/g++.

0

Install gcc and try the video below.
Try this:
https://www.youtube.com/watch?v=A6v2Ceqy4Tk
Hope it will works for you.

prappo prince
  • 107
  • 1
  • 6