LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   gcc Linker error - please help! (http://www.linuxquestions.org/questions/programming-9/gcc-linker-error-please-help-365961/)

Liche 09-22-2005 11:48 AM

gcc Linker error - please help!
 
Am starting out in gcc/linux and am porting an application from MSVCPP6 into gcc (yes, I've finally seen the light...). I've hit a number of problems and figured them out, but this one has me stumped.

The program compiles fine, but on linking I get a ton of errors like this:
Code:

/tmp/cckguNaH.o(.gnu.linkonce.t._ZNSt4listIP11HornElementSaIS1_EEaSERKS3_+0x67): In function `std::list<HornElement*, std::allocator<HornElement*> >::operator=(std::list<HornElement*, std::allocator<HornElement*> > const&)':

: undefined reference to `int operator!=<std::_List_iterator<HornElement*, HornElement*&, HornElement**> >(std::_List_iterator<HornElement*, HornElement*&, HornElement**> const&, std::_List_iterator<HornElement*, HornElement*&, HornElement**> const&)'

(HornElement is a class in the program). All the errors refer to 'list' in some way - I'm using the STL list template. I've tried linking in with -lstdc++ and --static -lstdg++ but this makes no difference.

The application requries GALib (standard genetic algorithm library), which I'm linking to - its examples run fine, and I've checked the include/link phases with short test programs and they compile/link/build/run fine - I don't think the problem is here but thought I'd mention it. This is what the -lga command links in. (I installed and 'make'd this myself, and this is a new concept for me so it is conceivable that I got it wrong, but I don't think so as I've successfully linked things to libga.a)

Here is the build command:

g++ main.cpp -Wall -I../include -L../lib -lga -lm

(The -I and -L paths are correct and have been verified separately). I should mention that the program is spread over a number of other .h/.cpp files I wrote which are #included in main.cpp - this all compiles fine.

'g++ -v' gives:

Code:

Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-52)

Running
g++ main.cpp -shared -Wall -I../include -L../lib -lga -lm
compiles and links, but on running ./a.out immediately crashes with 'Segmentation Fault''

I'm utterly stuck on this and will be very grateful for any help; I'll gladly provide any more information that I have omitted here.

Thanks

:newbie:

JCipriani 09-22-2005 03:48 PM

Looks like you have a != operator declared somewhere that you aren't actually defining. Did you write HornElement? Are you using it in a way where you have to explicitly instantiate a HornElement but you aren't, because the MS compiler let it slide?

Also, check this out. It's a handy utility that will take weird error messages involving templates and STL objects and "decode" them into more easily readable messages.

Quote:

Am starting out in gcc/linux and am porting an application from MSVCPP6 into gcc (yes, I've finally seen the light...).
PS: Don't get your hopes up just yet. VS 6 has, honestly, one of the best C/C++ IDE's that I've ever worked with. Mostly because it's debugger beats gdb+ddd any day. Too bad the editor isn't like xemacs though ;)

Liche 09-23-2005 03:03 AM

Thanks for your response. The error messages all refer to either != or == operators on _List_iterator; the program implements list<HornElement*>. I have tried adding an explicit instantiation
Code:

template class list<HornElement*>
in various places (can't say I fully understand what I'm doing with this) but it doesn't seem to help. HornElement is my code - exactly when would it 'need' instantiation? Exactly how should I go about doing this?

Many thanks.

foo_bar_foo 09-24-2005 11:17 PM

the -shared flag tells the linker to create a shared library (*.so) not an executable
Quote:

I should mention that the program is spread over a number of other .h/.cpp files I wrote which are #included in main.cpp - this all compiles fine.
this is what's comfusing
you need to compile them other .cpp files into objects
and then link them all together
g++ -c main.cpp
g++ -o programname main.o otherobject.o otherobject2.o -I../include -L../lib -lga -lm

time to write yourself a Makefile !

Liche 09-26-2005 06:11 AM

Thanks for the tip - have tried compiling piecemeal and linking the objects together, but the error messages remain.

I now don't think this is directly a problem with linking; I suspect there is some error in my STL usage that is thrown during linking.

Do I need to define != and == operators on T to use std::_List_iterator<T> ? If so, where?

(none of this came up with MSVCPP6 ):confused:

Liche 09-26-2005 07:14 AM

Have fixed the problem. Turns out there was some wierd conflict with GALib - including certain header files affected how gcc treated list<T>::iterator and const_iterator in const functions. Don't fully understand the exact reason for the problem but appear to have fixed it satisfactorily. Thanks for your help.


All times are GMT -5. The time now is 02:49 PM.