{"id":241,"date":"2008-09-22T17:39:46","date_gmt":"2008-09-22T21:39:46","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/?p=241"},"modified":"2008-09-22T17:39:46","modified_gmt":"2008-09-22T21:39:46","slug":"when-linking-the-order-of-your-command-line-can-be-important","status":"publish","type":"post","link":"https:\/\/benjamin.smedbergs.us\/blog\/2008-09-22\/when-linking-the-order-of-your-command-line-can-be-important\/","title":{"rendered":"When linking, the order of your command-line can be important"},"content":{"rendered":"<p>Occasionally, people will come on the #xulrunner or #extdev channel with a question about compiling XPCOM components. The question often goes something like this:<\/p>\n<blockquote><p>&lt;IRCGuy&gt; I&#8217;m following a tutorial on making XPCOM components, but I can&#8217;t seem to get them to compile. Can anyone tell me what my problem is?<\/p><\/blockquote>\n<p>Hint for asking a good question: IRCGuy needs to tell us some combination of 1) what tutorial he&#8217;s following, 2) what the failing command is or 3) what the error message is.<\/p>\n<p>This time, IRCGuy&#8217;s compile command and error message are:<\/p>\n<blockquote>\n<pre>IRCGuy@IRCGuy-france:\/mnt\/data\/IRCGuy\/public\/project\/xpcom-test$ make\r\ng++  -I\/usr\/local\/include\/xulrunner-1.9\/unstable -I\/usr\/local\/include\/xulrunner-1.9\/stable -L\/usr\/local\/lib\/xulrunner-devel-1.9\/lib -Wl,-rpath-link,\/usr\/local\/bin  -lxpcomglue_s -lxpcom -lnspr4 -fno-rtti -fno-exceptions -shared -Wl,-z,defs  france2.cpp -o france2.so\r\n\/tmp\/cceFg2dD.o: In function `NSGetModule':\r\nfrance2.cpp:(.text+0x38c): undefined reference to `NS_NewGenericModule2(nsModuleInfo const*, nsIModule**)'<\/pre>\n<\/blockquote>\n<p>IRCGuy&#8217;s problem is a problem of <em>link ordering<\/em>: with most unix-like linkers, it is very important to list object files and libraries in the correct order. The general order you want to follow is as follows:<\/p>\n<ol>\n<li>Object files\n<li>Static libraries &#8211; specific to general\n<li>Dynamic libraries\n<\/ol>\n<p>If an object file needs a symbol, the linker will only resolve that symbol in static libraries that are <em>later<\/em> in the link line.<\/p>\n<p>The corrected command:<\/p>\n<pre>g++  -I\/usr\/local\/include\/xulrunner-1.9\/unstable -I\/usr\/local\/include\/xulrunner-1.9\/stable -fno-rtti -fno-exceptions -shared -Wl,-z,defs  france2.cpp -L\/usr\/local\/lib\/xulrunner-devel-1.9\/lib -Wl,-rpath-link,\/usr\/local\/bin  -lxpcomglue_s -lxpcom -lnspr4 -o france2.so<\/pre>\n<p><em>Bonus tip:<\/em> correct linker flags for linking XPCOM components can be found on the Mozilla Developer Center article on the <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/XPCOM_Glue\">XPCOM Glue<\/a>. As noted in the article, xpcom components want to use the &#8220;Dependent Glue&#8221; linker strategy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Occasionally, people will come on the #xulrunner or #extdev channel with a question about compiling XPCOM components. The question often goes something like this: &lt;IRCGuy&gt; I&#8217;m following a tutorial on making XPCOM components, but I can&#8217;t seem to get them to compile. Can anyone tell me what my problem is? Hint for asking a good [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[30,38],"class_list":["post-241","post","type-post","status-publish","format-standard","hentry","category-mozilla","category-untagged","tag-build-system","tag-xpcom"],"_links":{"self":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":0,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"wp:attachment":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}