{"id":255,"date":"2022-08-30T14:58:18","date_gmt":"2022-08-30T14:58:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/gcc-static-library-external-assembly-function-becomes-undefined-symbol-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:58:18","modified_gmt":"2022-08-30T14:58:18","slug":"gcc-static-library-external-assembly-function-becomes-undefined-symbol-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/gcc-static-library-external-assembly-function-becomes-undefined-symbol-record-and-share-programming-errors\/","title":{"rendered":"gcc, static library, external assembly function becomes undefined symbol-Record and share programming errors"},"content":{"rendered":"<p>I have a problem with g++ building an application which links to a static library, where the latter shall contain some global functions written in external asm-files, compiled with yasm. So in the library, I have<\/p>\n<pre><code>#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nextern void __attribute__((cdecl)) interp1( char *pSrc );\nextern void __attribute__((cdecl)) interp2( char *pSrc );\n\n#ifdef __cplusplus\n}\n#endif\n<\/code><\/pre>\n<p>which I reference elsewhere inside the library. Then, there is the implementation in an asm-file, like this:<\/p>\n<pre><code>section .data\n; (some data)\nsection .text\n; (some text)\n\nglobal _interp1\n_interp1:\n    ; (code ...)\n    ret\n\nglobal _interp2\n_interp2:\n    ; (code ...)\n    ret\n<\/code><\/pre>\n<p>Compiling and Linking work fine for the library, I do<\/p>\n<pre><code>yasm -f elf32 -O2 -o interp.o interp.asm\n<\/code><\/pre>\n<p>and then<\/p>\n<pre><code>ar -rc libInterp.a objs1.o [...] objsN.o interp.o \nranlib libInterp.a\n<\/code><\/pre>\n<p>Now finally, to link the library to the main application, I do<\/p>\n<pre><code>g++ -O4 -ffast-math -DNDEBUG -fomit-frame-pointer -DARCH_X86 -fPIC -o ..\/bin\/interp this.o that.o -lboost_thread -lpthread .\/libInterp.a \n<\/code><\/pre>\n<p>and I get the errors<\/p>\n<pre><code>undefined reference to `interp1'\nundefined reference to `interp2'\n<\/code><\/pre>\n<p>What am I doing wrong here? any help is appreciated.<\/p>\n<ol>\n<li>\n<p>Depending on the target type, gcc will not prepend a leading underscore to external symbols. It appears that this is the case in your scenario.<\/p>\n<p>The simple fix is probably to remove the underscores from the names in your assembly file.<\/p>\n<p>A couple alternatives you might consder might be to use something like one of the following macros for your symbols in the assembly file:<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-08-31 06:53:26. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a problem with g++ building an application which links to a static library, where the latter shall contain some global functions written in external asm-files, compiled with yasm. So in the library, I have #ifdef __cplusplus extern &#8220;C&#8221; { #endif extern void __attribute__((cdecl)) interp1( char *pSrc ); extern void __attribute__((cdecl)) interp2( char *pSrc [&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-255","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/255","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=255"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}