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;
}
It's present in all Linux/Unix distributions. gcc(GNU Compiler Collection) is one of the most widely used C compilers . Ubuntu uses gcc and is installed by default when you install it on your system. Type gcc <filename> and g++ filename on the terminal to compile C and C++ programs respectively.
You need to use the which command to locate c compiler binary called gcc. Usually, it is installed in /usr/bin directory.
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
.
You probably should use g++ rather than gcc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With