Linking shared lib with GCC - undefined symbol

Joined
Sep 30, 2008
Messages
2
Reaction score
0
Hi everyone,

I am encountering a problem while linking a shared library with GCC 4.1 on linux.
I have written a static library (implementing some utils function), containing utils.h and utils.cpp

Here is the part of the makefile creating the static lib named libutils.a :

Code:
libutils.a: utils.o
        ar -r $@ $^
        ranlib $@

utils.o: utils.h utils.cpp
        g++ -static-libgcc -Wall -ansi -I. -o $@ -c utils.cpp

I'm making a test exe , which makefile is :
Code:
main_test: test_utils.o libutils.a

test_utils.o: test_utils.h test_utils.cpp
        g++ -static-libgcc -Wall -ansi -I. -o $@ -c test_utils.cpp

And a shared library to use with JNI :

Code:
libjniutils.so: jni_utils.o
        g++ -shared -o $@ $^ ./libutils.a

jni_utils.o: jni_utils.h jni_utils.cpp
        g++ -static-libgcc -Wall -ansi -I. -o $@ -c jni_utils.cpp


Both the test exe and the jni shared library work perfectly.
Now (here comes the problem ...) I want to add a new file to my libutils.a, so I modify the makefile to :

Code:
libutils.a: utils.o string_utils.o
        ar -r $@ $^
        ranlib $@

utils.o: utils.h utils.cpp
        g++ -static-libgcc -Wall -ansi -I. -o $@ -c utils.cpp
string_utils.o: string_utils.h string_utils.cpp
        g++ -static-libgcc -Wall -ansi -I. -o $@ -c string_utils.cpp

This should be the only thing I have to modify. However now the test exe runs perfectly, but JNI does not want.
After some investigations, I found that two of the functions from string_utils I use in the jni shared library are undefined :

Code:
dev@rantanplan:~/DEV$ ldd -d libjniutils.so
undefined symbol: _ZN11StringUtils4trimERKSsS1_ (libjniutils.so)
undefined symbol: _ZN11StringUtils7toUpperERKSs (libjniutils.so)
        linux-gate.so.1 =>  (0xffffe000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7cdc000)
        libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7cb7000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7cac000)
        libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7b7b000)
        /lib/ld-linux.so.2 (0x80000000)

However, doing nm -s on libutils.a gives :

Code:
dev@rantanplan:~/DEV$ nm -s libutils.a | grep StringUtils
_ZN11StringUtils7toUpperERKSs in string_utils.o
_ZN11StringUtils7toLowerERKSs in string_utils.o
_ZN11StringUtils8trimLeftERKSsS1_ in string_utils.o
_ZN11StringUtils9trimRightERKSsS1_ in string_utils.o
_ZN11StringUtils4trimERKSsS1_ in string_utils.o
0000030c T _ZN11StringUtils4trimERKSsS1_
000000ba T _ZN11StringUtils7toLowerERKSs
00000000 T _ZN11StringUtils7toUpperERKSs
00000174 T _ZN11StringUtils8trimLeftERKSsS1_
0000020a T _ZN11StringUtils9trimRightERKSsS1_

I've been looking for any error but couldn't find anything. I'm sure it is a small thing, but I cannot see it. Hope you would light me up :)

Thanks

nutzzz
 
Joined
Sep 30, 2008
Messages
2
Reaction score
0
some little clue ...
If I force to include string_utils.o in the link operation in the makefile, the problem is solved.
Also, if I make a "ar -x libutils.a", both utils.o and string_utils.o files are extracted.
So why doesn't the linker cannot do the job ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top