{"id":1780,"date":"2023-04-15T18:43:58","date_gmt":"2023-04-15T17:43:58","guid":{"rendered":"https:\/\/coding.moris.org\/?p=1780"},"modified":"2023-04-15T22:52:44","modified_gmt":"2023-04-15T21:52:44","slug":"placement-of-l-options-matter-in-gcc","status":"publish","type":"post","link":"https:\/\/priscimon.net\/coding\/2023\/04\/15\/placement-of-l-options-matter-in-gcc\/","title":{"rendered":"Placement of -l options matters in GCC"},"content":{"rendered":"\n<p>I ran into an interesting puzzle with GCC this afternoon when trying to compile the code below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;math.h&gt;\n#include &lt;stdio.h&gt;\n\nint main(int argc, char **argv)\n{\n    long double x = 2;\n    long double y = 1024;\n\n    long double result = powl(x, y);\n    printf(\"%LF ^ %LF : %LF\\n\", x, y, result);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>The first command that I used to compile the code failed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jeyoung@LENNY:~\/Temp$ cc -lm -o powltest powltest.c\n\/usr\/bin\/ld: \/tmp\/ccfqvvbz.o: in function `main':\npowltest.c:(.text+0x2e): undefined reference to `powl'\ncollect2: error: ld returned 1 exit status\njeyoung@LENNY:~\/Temp$<\/code><\/pre>\n\n\n\n<p>After about one hour of trying to resolve this error, I was resigned to reading the documentation. This is what I found from the section <em><a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Link-Options.html#Link-Options\">Options for Linking<\/a><\/em>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>-l <var>library<\/var><\/code><\/p>\n\n\n\n<p>Search the library named <var>library<\/var> when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)<\/p>\n\n\n\n<p>The <samp>-l<\/samp> option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.<\/p>\n\n\n\n<p>The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with <samp>-L<\/samp>.<\/p>\n\n\n\n<p>Static libraries are archives of object files, and have file names like <samp>lib<var>library<\/var>.a<\/samp>. Some targets also support shared libraries, which typically have names like <samp>lib<var>library<\/var>.so<\/samp>. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the <samp>-static<\/samp> option is used.<\/p>\n\n\n\n<p><strong>It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, \u00e2\u20ac\u02dc<samp>foo.o -lz bar.o<\/samp>\u00e2\u20ac\u2122 searches library \u00e2\u20ac\u02dc<samp>z<\/samp>\u00e2\u20ac\u2122 after file <samp>foo.o<\/samp> but before <samp>bar.o<\/samp>. If <samp>bar.o<\/samp> refers to functions in \u00e2\u20ac\u02dc<samp>z<\/samp>\u00e2\u20ac\u2122, those functions may not be loaded.<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>After changing the position of <code>-lm<\/code>, the code was compiled successfully.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jeyoung@LENNY:~\/Temp$ cc -o powltest powltest.c -lm\njeyoung@LENNY:~\/Temp$ .\/powltest\n2.000000 ^ 1024.000000 : 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216.000000\njeyoung@LENNY:~\/Temp$<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I ran into an interesting puzzle with GCC this afternoon when trying to compile the code below. The first command that I used to compile the code failed: After about one hour of trying to resolve this error, I was resigned to reading the documentation. This is what I found from the section Options for [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1780","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3I4g9-sI","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/1780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/comments?post=1780"}],"version-history":[{"count":2,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/1780\/revisions"}],"predecessor-version":[{"id":1782,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/1780\/revisions\/1782"}],"wp:attachment":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/media?parent=1780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/categories?post=1780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/tags?post=1780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}