tags:

views:

388

answers:

2

I'm working on a library that must compile on linux and mac os X. Until now, I had no problem, compiling with "-g" worked well under both OS.

I tried to compile with some optimization ("-O2") and it works well under linux but I get an Undefined Symbol when I try to link a program with my library under mac os X.

Does anyone have any clue what I should look for?

nm mylib.a | grep _the_symbol

This returns same thing for linux and mac (no leading underscore under linux) :

154:00000018 C _the_symbol
377:         U _the_symbol

Here is the compile line under linux for the program using the library:

/usr/bin/gcc  -std=c99   CMakeFiles/prod-cons.dir/prod-cons.c.o  -o prod-cons -rdynamic -L/home/claferri/dev/build/src ../src/libckaapi.a -lpthread -Wl,-rpath,/home/claferri/dev/build/src

And under mac :

/usr/bin/gcc  -std=c99 -Wl,-search_paths_first -headerpad_max_install_names -fPIC CMakeFiles/prod-cons.dir/prod-cons.c.o  -o prod-cons  -L/Volumes/Data/claferri/Work/build/src ../src/libckaapi.a /usr/lib/libpthread.dylib
A: 

Note that the following is a guess, and I can't say for certain unless/until you provide the exact compiler flags you're using -- but Xcode defaults to setting -fvisibility=hidden, which would hide pretty much any symbol in your library, unless it's declared as visible.

You can do the same on Linux, but GCC's default is not to hide symbols.

You'll find more information here: http://gcc.gnu.org/wiki/Visibility

I don't use XCode to compile but CMake on both OS.
claferri
+1  A: 

Here's a guess at a workaround: try building the library with the -fno-common flag. If you have multiple definitions of this variable, you'll need to add "extern" to all but one.

Lance Richardson
I had some variable defined multiple times and using -fno-common was very helpfull to see that (in fact not in my part of the code so not obvious) but the funny thing is that it wasn't the variable that was undefined at first ... Seems it now compile with extern definition of those other variables. Thank you!
claferri
in fact I still get the same error if I remove the -fno-common ...I don't get why it works on linux and not on mac ...
claferri
My other thought was that it might be related to the order of the *.o files in the *.a archive. Could you post output from "nm -A mylib.a | grep the_symbol"? Perhaps moving the object file containing the definition after the object file containing the reference will help the linker do the right thing.
Lance Richardson