{"id":2142,"date":"2016-10-01T00:01:53","date_gmt":"2016-10-01T07:01:53","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2142"},"modified":"2016-10-08T08:21:35","modified_gmt":"2016-10-08T15:21:35","slug":"link-in-the-math-library","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2142","title":{"rendered":"Link in the Math Library"},"content":{"rendered":"<p>Recently, I had question posed to from a reader from Norway. He was working his way through my book and found that one of the programs wouldn&#8217;t compile on his Linux computer. The problem was that the C language math library isn&#8217;t automatically linked. The effect is that a linker error appears upon compiling.<br \/>\n<!--more--><br \/>\nWeird thing: I get the same error on my Linux laptop. It was completely unexpected!<\/p>\n<p>Here is sample code, which comes from a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1968\">blog post on June 18, 2016<\/a>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    double t;\r\n\r\n    for(t=1.0;t&lt;=10.0;t++)\r\n        printf(\"2 to the %.f power is %.f\\n\",\r\n                t,\r\n                pow(2.0,t));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I compiled at the command prompt using the following command:<\/p>\n<pre><code>clang -Wall 0618.c<\/code><\/pre>\n<p>The <code>-Wall<\/code> switch displays all errors, which is the default in Code::Blocks. Here&#8217;s the compiler output:<\/p>\n<pre><code>\/tmp\/0618-e49f59.o: In function 'main':\r\n0618.c(.text+0x4d): undefined reference to `pow'\r\nclang; error: linker command failed with exit code 1 (use -v to see invocation)<\/code><\/pre>\n<p>Ugh.<\/p>\n<p>The <em>clang<\/em> program is both a compiler and linker. The source code is compiled into object code and, upon success, linked into a program file. Clang then removes the object code file, <code>0618.o<\/code> in this example.<\/p>\n<p>Back in the old days, two programs handled the process, <em>cc<\/em> and <em>ld<\/em>, the compiler and linker, respectively. Today, both <em>clang<\/em> and <em>gcc<\/em> (and <em>cc<\/em>) automatically call the <em>ld<\/em> program, the linker, to complete the task. That&#8217;s when the error happens because the <em>pow()<\/em> function lacks a definition &mdash; at least in the Linux computer&#8217;s standard C library.<\/p>\n<p>On both the PC and the Mac, the math library is included in the standard C library &mdash; at least it appears so. Otherwise, the compiler automatically calls in the library. But in Linux, apparently, you must manually specify that the linker include the math library.<\/p>\n<p><em>But isn&#8217;t the math library included with <code>#include &lt;math.h&gt;<\/code><\/em><\/p>\n<p>No!<\/p>\n<p>If I comment out the <code>#include<\/code> directive, you get a compiler warning. Clang recommends that you include the <code>math.h<\/code> header file (which is one reason that I like and recommend that compiler), but you still see the linker error and the program doesn&#8217;t compile.<\/p>\n<p>The solution is to use the <code>-lx<\/code> switch to direct <em>clang<\/em> to include the math library.<\/p>\n<p>Libraries are typically stored in the <code>\/usr\/lib<\/code> folder in Unix, though the location can vary depending on the compiler. The math library goes by various names, but its abbreviation is <code>m<\/code> (lowercase M). To direct the linker to include the library, you must specify a linker switch when you compile:<\/p>\n<pre><code>clang -lm 0618.c<\/code><\/pre>\n<p>And the program finally compiles and links.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2159\">next week&#8217;s Lesson<\/a> I explore the process of adding a library in Code::Blocks, which is actually far more involved than using a command line switch, even if that&#8217;s what Code::Blocks does internally.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apparently some Linux releases don&#8217;t automatically compile some C Library math functions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2142\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2142","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2142"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2142\/revisions"}],"predecessor-version":[{"id":2171,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2142\/revisions\/2171"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}