{"id":951,"date":"2022-08-30T15:09:54","date_gmt":"2022-08-30T15:09:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/stripping-weak-or-undefined-symbols-in-gcc-a-and-so-libraries-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:54","modified_gmt":"2022-08-30T15:09:54","slug":"stripping-weak-or-undefined-symbols-in-gcc-a-and-so-libraries-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/stripping-weak-or-undefined-symbols-in-gcc-a-and-so-libraries-collection-of-common-programming-errors\/","title":{"rendered":"Stripping Weak or Undefined Symbols in gcc .a and .so libraries-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to deal with an undefined symbol problem in linux. I&#8217;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)<\/p>\n<p>First, I have built GDCM as static libraries (libgdcmMSFF.a, for example). No problem here. Then, I created my custom code \u2013 myGDCM.cc \u2013 which has various calls into GDCM. Then I run my Makefile:<\/p>\n<pre><code>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 \n\ng++ -shared -rdynamic -fPIC -O3 -DNDEBUG -lpthread -ldl -lc -lm \n\/dev\/GDCM-2.2\/lib\/libgdcmMSFF.a \n\/dev\/GDCM-2.2\/lib\/libgdcmCommon.a \n\/dev\/GDCM-2.2\/lib\/libgdcmDICT.a \n\/dev\/GDCM-2.2\/lib\/libgdcmDSED.a \n\/dev\/GDCM-2.2\/lib\/libgdcmIOD.a \n\/dev\/GDCM-2.2\/lib\/libgdcmMEXD.a \n\/dev\/GDCM-2.2\/lib\/libgdcmcharls.a \n\/dev\/GDCM-2.2\/lib\/libgdcmexpat.a \n\/dev\/GDCM-2.2\/lib\/libgdcmuuid.a \n\/dev\/GDCM-2.2\/lib\/libgdcmjpeg12.a \n\/dev\/GDCM-2.2\/lib\/libgdcmjpeg16.a \n\/dev\/GDCM-2.2\/lib\/libgdcmjpeg8.a \n\/dev\/GDCM-2.2\/lib\/libgdcmopenjpeg.a \n\/dev\/GDCM-2.2\/lib\/libgdcmzlib.a \n\/dev\/GDCM-2.2\/lib\/libsocketxx.a \n\/dev\/GDCM-2.2\/lib\/libgdcmMSFF.a \n\u2013o libmyGDCM.so OBJECTS\/AMD_LINUX64\/myGDCM.o\n<\/code><\/pre>\n<p>It compiles fine, but when I load my new .so from an application, it throws an error:<\/p>\n<p>couldn&#8217;t load file \u201clibmyGDCM.so&#8221;: libmyGDCM.so: undefined symbol: _ZTVN4gdcm6PixmapE<\/p>\n<p>So, then I looked at the .so (nm libmyGDCM.so) and found:<\/p>\n<pre><code>     U _ZTVN4gdcm6PixmapE\n<\/code><\/pre>\n<p>which I understand to mean that it is undefined. More specifically, I found tried (nm libmyGDCM.so \u2013demangle) and got:<\/p>\n<pre><code>     U vtable for gdcm::Pixmap\n<\/code><\/pre>\n<p>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<\/p>\n<pre><code>     0000000000000000 V _ZTVN4gdcm6PixmapE\n<\/code><\/pre>\n<p>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:<\/p>\n<ol>\n<li>The Pixmap class has a pure virtual constructor or destructor<\/li>\n<li>When compiled into a .a, it becomes a weak symbol<\/li>\n<li>When compiled into a .so, it becomes undefined<\/li>\n<li>Attempting to load a .so with an undefined symbol is a bad thing.<\/li>\n<\/ol>\n<p>First, can anyone confirm that any of this makes sense? Second, any reasonable suggestions for dealing with this? Specifically, I couldn\u2019t find a way to strip weak symbols from a .a file or undefined symbols from a .so file. There isn\u2019t much info on this, but one post suggested that gcc didn\u2019t support this.<\/p>\n<p>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.<\/p>\n<p>I appreciate any help.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:05:51. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to deal with an undefined symbol problem in linux. I&#8217;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). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-951","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}