PDA

View Full Version : Compiling C++ code


bman
2nd February 2007, 02:05 PM
Hey,

I'm new to c++ although I have a good bit of C programming knowledge. I am trying to compile a very simple program involving classes. Here it is:

// classes example
#include <iostream>
using namespace std;

class CRectangle
{
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b)
{
x = a;
y = b;
}

int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}

When I compile with the command show below I get the following errors:

gcc main.cpp -o main.exe

/tmp/ccXJ5ekd.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x39): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccXJ5ekd.o: In function `__tcf_0':
main.cpp:(.text+0x82): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccXJ5ekd.o: In function `main':
main.cpp:(.text+0xcd): undefined reference to `std::cout'
main.cpp:(.text+0xd2): 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*)'
main.cpp:(.text+0xde): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
/tmp/ccXJ5ekd.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Does anyone know what the problem is? My compiler is up to date as far as I can tell. Heres the output of gcc -v:

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)

Hope someone can help. Thanks.

sideways
2nd February 2007, 02:24 PM
use g++ to compile

(.exe is nonstandard as an extension for executables in linux, but that won't affect the compilation)

bman
2nd February 2007, 02:24 PM

I'm an idiot!! I was using gcc instead of g++, Sorry for wasting a thread space.

iwasapenguin
10th February 2007, 03:43 AM
Actually I'm pretty sure g++ is a gcc fontend and that gcc can tell the difference because I've got the same errors from both.

sideways
10th February 2007, 12:08 PM
Actually I'm pretty sure g++ is a gcc fontend and that gcc can tell the difference because I've got the same errors from both.

gcc used to be a c compiler (gnu c compiler), now, confusingly it means gnu compiler collection and will compile c, c++, fortran, java and more. The problem is that it doesn't know how to link c++ code automatically, you need to manually specify the linking options if you use gcc to compile a c++ program (usually)

g++ is the gnu c++ compiler, it correctly links the standard c++ libraries before calling gcc, hence often 'g++ program.cpp' will work whereas 'gcc program.cpp' will often fail

g++ should compile that code without problems, if you're getting errors then you must have g++ incorrectly installed