{"id":512,"date":"2022-08-30T15:02:35","date_gmt":"2022-08-30T15:02:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/strange-behaviour-of-gcc-and-math-h-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:35","modified_gmt":"2022-08-30T15:02:35","slug":"strange-behaviour-of-gcc-and-math-h-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/strange-behaviour-of-gcc-and-math-h-collection-of-common-programming-errors\/","title":{"rendered":"Strange behaviour of gcc and math.h?-Collection of common programming errors"},"content":{"rendered":"<p>Yes, it is normal. For many linkers, the order in which you specify the object files and the libraries matters.<\/p>\n<p>To quote &#8220;An Introduction to GCC &#8211; for the GNU compilers gcc and g++&#8221;:<\/p>\n<blockquote>\n<p>The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. This means that a library containing the definition of a function should appear after any source files or object files which use it. This includes libraries specified with the short-cut <code>-l<\/code> option, as shown in the following command:<\/p>\n<p><code>$ gcc -Wall calc.c -lm -o calc (correct order)<\/code><\/p>\n<\/blockquote>\n<p>This behaviour is common, but is by no means universal. When in doubt, it is best to consult your linker&#8217;s manual. For example, on my Ubuntu system <code>man ld<\/code> states that:<\/p>\n<pre><code>   -l namespec\n   --library=namespec\n\n       ...\n\n       The linker will search an archive only once, at the location where\n       it is specified on the command line.  If the archive defines a\n       symbol which was undefined in some object which appeared before the\n       archive on the command line, the linker will include the\n       appropriate file(s) from the archive.  However, an undefined symbol\n       in an object appearing later on the command line will not cause the\n       linker to search the archive again.\n<\/code><\/pre>\n<p>In other words, this linker does behave in the manner described in the <code>gcc<\/code> book.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:02:36. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Yes, it is normal. For many linkers, the order in which you specify the object files and the libraries matters. To quote &#8220;An Introduction to GCC &#8211; for the GNU compilers gcc and g++&#8221;: The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the [&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-512","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/512","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=512"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}