Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q] g++ header files



   From: Craig Oda <craig@example.com>

   Hi,
   I thought I would get our renewed discussion going with a question
   on g++

   I usually use standard c, but I went to the U.S. and bought a book
   on c++ and tried to go through the first example "hello world" program
   to make sure the compiler was working.  I am having a little trouble:

     cow:~/practice/c/cpp% g++ -I/usr/include/g++ hello.cc
     /tmp/cca001071.o(.text+0x1e): undefined reference to `cout'
     /tmp/cca001071.o(.text+0x23): undefined reference to 
     `ostream::operator<<(char const *)'

This is a standard G++ gotcha.  For efficiency reasons, the stream
operators are implemented *inline*.  That is, the machine code is
substituted directly into the object file, and you save the overhead
of saving registers, a "call" instruction, the "ret" instruction, and
restoring registers.  When you consider that many stream operations
can be implemented in *two* assembly instructions (move from memory to 
register, advance buffer pointer), you can see that this is a BigWin.

The "gotcha" is that G++ only performs correctly under optimization.
So you must compile with "g++ -O2 ...".

You may also run into a problem with not having the stream library,
you may need to add "-liostream" or "-lgpp" or something like that.
However, I think that "g++ ..." should do this automatically.

Steve




Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links