views:

99

answers:

2

Unfortunately yes.

I have my shared library compiled, the linker doesn't complain about not finding it but still I get undefined reference error. Thinking that I might be doing something wrong I did a little research and found this nice, simple walkthrough:

http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

which I've followed to the letter but still I get:

$ gcc -Wall main.c -o dynamically_linked  -L.\ -lmean
/tmp/ccZjkkkl.o: In function `main':
main.c:(.text+0x42): undefined reference to `mean'
collect2: ld returned 1 exit status

This is pretty simple stuff so what's going wrong?! Is there something in my set up that might need checking/tweeking?

GCC 4.3.2 Fedora 10 64-bit

+3  A: 

Change:

$ gcc -Wall main.c -o dynamically_linked  -L.\ -lmean

to:

$ gcc -Wall main.c -o dynamically_linked  -L. -lmean



You probably meant to do this:

$ gcc -Wall main.c -o dynamically_linked  -L./ -lmean

which is OK, but the trailing / is redundant

Paul R
Agreed but...$ gcc -Wall main.c -o dynamically_linked -L./ -lmean/usr/bin/ld: cannot find -lmeancollect2: ld returned 1 exit statusand...$ gcc -Wall main.c -o dynamically_linked -L. -lmean/usr/bin/ld: cannot find -lmeancollect2: ld returned 1 exit statusSo now ld can't seem to find the library whereas with$ gcc -Wall main.c -o dynamically_linked -L.\ -lmean/tmp/cceWR2jQ.o: In function `main':main.c:(.text+0x42): undefined reference to `mean'collect2: ld returned 1 exit statusSeems to be saying 'library found' but refuse to link :(Weird.
roony
@roony: no - the \ character you used originally escapes the next character (space in this case) so you were actually just passing `". -lmean"` as the path to `-L`. You are now passing a library with the corrected command line, but it's not being found. It should be named `libmean.so` or similar.
Paul R
Yup! The library is libmean.so yet [[$ gcc -Wall main.c -o dynamically_linked -L -lmean]] still yields undefined ref to `mean' in main arghh!
roony
@roony: is that a typo or do you really not have a `.` after the `-L` ?
Paul R
err.. I did but that's gone now. My current attempt is exactly as shown in my previous comment. It's utterly bizarre. I'm scanning through old makefiles for clues but nothing I try works!
roony
@roony: am I right in thinking that libmean.so is in the current directory ? If so then you need `-L.` to tell the linker where to find it (`.` means the current directory). If not then you need to put the appropriate path after `-L`.
Paul R
A: 

How was the library created? Libtool?

Show us an ls -l of your current directory

pm100