[oclug] g++ linking question

James Leigh james.leigh at ottawa.com
Fri Jan 26 16:21:37 EST 2001


fellas,

I am having the darndest time trying to use g++ and compile a simple
program.  In the past I have always started with one cpp file and included
anything I needed, but as programs get bigger this method causes problems. 
Now I want to compile objects and link them together.

I have a main.cpp a header file and a class definition file.  When I try to
run g++ on the two cpp files is spits out the same errors as if all I was
compiling was the main.cpp.  Here is some shell output to help explain what
is going on.

 ----------------------------------------------------------------
 | these files are in early stage, not ready to be used,                   
                                |
 | I would not recommend using them..... and all that .. I take no
responsiblity for them ....    |
 ----------------------------------------------------------------

###############################################

$ g++ main.cpp              
/tmp/ccTLJBe1.o: In function `main':
/tmp/ccTLJBe1.o(.text+0x21): undefined reference to
`Smartptr<char>::Smartptr(char *)'
/tmp/ccTLJBe1.o(.text+0x30): undefined reference to
`Smartptr<char>::operator*(void)'
/tmp/ccTLJBe1.o(.text+0x49): undefined reference to
`Smartptr<char>::operator*(void)'
/tmp/ccTLJBe1.o: In function `Smartptr<char>::~Smartptr(void)':
/tmp/ccTLJBe1.o(.gnu.linkonce.t._._t8Smartptr1Zc+0x14): undefined reference
to `Smartptr_ptr<char *>::~Smartptr_ptr(void)'
collect2: ld returned 1 exit status
 ###################################################
 # as there should be errors since smartptr is defined in smartptr.cpp
 #######################################################

$ g++ main.cpp smartptr.cpp 
/tmp/ccBLypWH.o: In function `main':
/tmp/ccBLypWH.o(.text+0x21): undefined reference to
`Smartptr<char>::Smartptr(char *)'
/tmp/ccBLypWH.o(.text+0x30): undefined reference to
`Smartptr<char>::operator*(void)'
/tmp/ccBLypWH.o(.text+0x49): undefined reference to
`Smartptr<char>::operator*(void)'
/tmp/ccBLypWH.o: In function `Smartptr<char>::~Smartptr(void)':
/tmp/ccBLypWH.o(.gnu.linkonce.t._._t8Smartptr1Zc+0x14): undefined reference
to `Smartptr_ptr<char *>::~Smartptr_ptr(void)'
collect2: ld returned 1 exit status
 #############################################
 # now this should not be the case, I would expect there to be no errors
here since
 # smartptr is defined in smartptr.cpp
 ############################################
 # at the top of main.cpp it says "#include"smartptr.h"
 # If I switch that with "#include"smartptr.cpp"
 # and run the first command again it says this
 #############################################

$ g++ main.cpp && echo success compiling
success compiling
 ############################################
 # no errors and produces and a.out file to run, and it runs correctly.
 ############################################

What is going on?  When I over simplify and use a new plain jane class it
can link the two cpp files together, but not with smartptr.cpp.  Since it
compiles just fine as show in the last command what am I doing wrong when I
try to link it?

here are the files I an using
http://chat.carleton.ca/~jleigh/gcc/main.cpp
http://chat.carleton.ca/~jleigh/gcc/smartptr.h
http://chat.carleton.ca/~jleigh/gcc/smartptr.cpp


thanks,
james



More information about the OCLUG mailing list