{"id":766,"date":"2022-08-30T15:06:49","date_gmt":"2022-08-30T15:06:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-include-static-library-in-makefile-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:49","modified_gmt":"2022-08-30T15:06:49","slug":"how-to-include-static-library-in-makefile-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-include-static-library-in-makefile-collection-of-common-programming-errors\/","title":{"rendered":"How to include static library in makefile-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<p>use<\/p>\n<pre><code>LDFLAGS= -L -l\n<\/code><\/pre>\n<p>Like :<\/p>\n<pre><code>LDFLAGS = -L. -lmine\n<\/code><\/pre>\n<p>for ensuring static compilation you can also add<\/p>\n<pre><code>LDFLAGS = -static\n<\/code><\/pre>\n<p>Or you can just get rid of the whole library searching, and link with with it directly.<\/p>\n<p>say you have main.c fun.c<\/p>\n<p>and a static library libmine.a<\/p>\n<p>then you can just do in your final link line of the Makefile<\/p>\n<pre><code>$(CC) $(CFLAGS) main.o fun.o libmine.a\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Make sure that the <code>-L<\/code> option appears ahead of the <code>-l<\/code> option; the order of options in linker command lines <em>does<\/em> matter, <em>especially<\/em> with static libraries. The <code>-L<\/code> option specifies a directory to be searched for libraries (static or shared). The <code>-lname<\/code> option specifies a library which is with <code>libmine.a<\/code> (static) or <code>libmine.so<\/code> (shared on most variants of Unix, but Mac OS X uses <code>.dylib<\/code> and HP-UX used to use <code>.sl<\/code>). Conventionally, a static library will be in a file <code>libmine.a<\/code>. This is convention, not mandatory, but if the name is not in the <code>libmine.a<\/code> format, you cannot use the <code>-lmine<\/code> notation to find it; you must list it explicitly on the compiler (linker) command line.<\/p>\n<p>The <code>-L.\/libmine<\/code> option says &#8220;there is a sub-directory called <code>libmine<\/code> which can be searched to find libraries&#8221;. I can see three possibilities:<\/p>\n<ol>\n<li>You have such a sub-directory containing <code>libmine.a<\/code>, in which case you also need to add <code>-lmine<\/code> to the linker line (after the object files that reference the library).<\/li>\n<li>You have a file <code>libmine<\/code> that is a static archive, in which case you simply list it as a file <code>.\/libmine<\/code> with no <code>-L<\/code> in front. Aga<\/li>\n<li>You have a file <code>libmine.a<\/code> in the current directory that you want to pick up. You can either write <code>.\/libmine.a<\/code> or <code>-L . -lmine<\/code> and both should find the library.<\/li>\n<\/ol>\n<\/li>\n<li>\n<p>The <code>-L<\/code> merely gives the path where to find the <code>.a<\/code> or <code>.so<\/code> file. What you&#8217;re looking for is to add <code>-lmine<\/code> to the <code>LIBS<\/code> variable.<\/p>\n<p>Make that <code>-static -lmine<\/code> to force it to pick the static library (in case both static and dynamic library exist).<\/p>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:41:13. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>use LDFLAGS= -L -l Like : LDFLAGS = -L. -lmine for ensuring static compilation you can also add LDFLAGS = -static Or you can just get rid of the whole library searching, and link with with it directly. say you have main.c fun.c and a static library libmine.a then you can just do in your [&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-766","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/766","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=766"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/766\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}