Stripping Weak or Undefined Symbols in gcc .a and .so libraries-Collection of common programming errors
I’m trying to deal with an undefined symbol problem in linux. I’m running RHEL6 with gcc 3.4.4. I get the same failures on both 32- and 64-bit platforms. I am trying to build a custom library which includes parts of GDCM (a DICOM library)
First, I have built GDCM as static libraries (libgdcmMSFF.a, for example). No problem here. Then, I created my custom code – myGDCM.cc – which has various calls into GDCM. Then I run my Makefile:
g++ -Os -c -I/dev/GDCM-2.2/include/gdcm-2.2 -fPIC -DAMD_LINUX64=1 -DENDIAN_LITTLE=1 -DSTDC_HEADERS -DNDEBUG -o OBJECTS/AMD_LINUX64/myGDCM.o myGDCM.cc
g++ -shared -rdynamic -fPIC -O3 -DNDEBUG -lpthread -ldl -lc -lm
/dev/GDCM-2.2/lib/libgdcmMSFF.a
/dev/GDCM-2.2/lib/libgdcmCommon.a
/dev/GDCM-2.2/lib/libgdcmDICT.a
/dev/GDCM-2.2/lib/libgdcmDSED.a
/dev/GDCM-2.2/lib/libgdcmIOD.a
/dev/GDCM-2.2/lib/libgdcmMEXD.a
/dev/GDCM-2.2/lib/libgdcmcharls.a
/dev/GDCM-2.2/lib/libgdcmexpat.a
/dev/GDCM-2.2/lib/libgdcmuuid.a
/dev/GDCM-2.2/lib/libgdcmjpeg12.a
/dev/GDCM-2.2/lib/libgdcmjpeg16.a
/dev/GDCM-2.2/lib/libgdcmjpeg8.a
/dev/GDCM-2.2/lib/libgdcmopenjpeg.a
/dev/GDCM-2.2/lib/libgdcmzlib.a
/dev/GDCM-2.2/lib/libsocketxx.a
/dev/GDCM-2.2/lib/libgdcmMSFF.a
–o libmyGDCM.so OBJECTS/AMD_LINUX64/myGDCM.o
It compiles fine, but when I load my new .so from an application, it throws an error:
couldn’t load file “libmyGDCM.so”: libmyGDCM.so: undefined symbol: _ZTVN4gdcm6PixmapE
So, then I looked at the .so (nm libmyGDCM.so) and found:
U _ZTVN4gdcm6PixmapE
which I understand to mean that it is undefined. More specifically, I found tried (nm libmyGDCM.so –demangle) and got:
U vtable for gdcm::Pixmap
This reference is from the libgdcmMSFF.a which I previously created. So, then I look at that: nm /dev/GDCM-2.2/lib/libgdcmMSFF.a | grep _ZTVN4gdcm6PixmapE
0000000000000000 V _ZTVN4gdcm6PixmapE
As I understand it, V indicates that it is a weak symbol. From all of my searching, the best that I can conclude is that:
- The Pixmap class has a pure virtual constructor or destructor
- When compiled into a .a, it becomes a weak symbol
- When compiled into a .so, it becomes undefined
- Attempting to load a .so with an undefined symbol is a bad thing.
First, can anyone confirm that any of this makes sense? Second, any reasonable suggestions for dealing with this? Specifically, I couldn’t find a way to strip weak symbols from a .a file or undefined symbols from a .so file. There isn’t much info on this, but one post suggested that gcc didn’t support this.
Also, I completed the same task with MSVC2008 and MSVC2010, and it appears that the compiler/linker does strip out these symbols, because it worked without any problems.
I appreciate any help.
Originally posted 2013-11-09 23:05:51.