{"id":249,"date":"2009-02-16T20:20:34","date_gmt":"2009-02-16T14:50:34","guid":{"rendered":"http:\/\/brainstorms.in\/?p=249"},"modified":"2009-02-16T20:20:34","modified_gmt":"2009-02-16T14:50:34","slug":"how-to-create-static-and-dynamic-libraries-with-gcc","status":"publish","type":"post","link":"https:\/\/brainstorms.in\/?p=249","title":{"rendered":"How to create static and dynamic libraries  with gcc"},"content":{"rendered":"<p>This is an exercise I did\u00a0\u00a0 today. I\u00a0 am documenting it for my own reference.<\/p>\n<p>The problem was simple.<\/p>\n<p>Create a library with functions.<\/p>\n<p>add(int,int) and sub (int,int)<\/p>\n<p>and then use the library\u00a0 in another program.<\/p>\n<p>Here is the code<\/p>\n<p>\/*add.c*\/<\/p>\n<p>#include &#8220;a.h&#8221;<\/p>\n<p>int add( int a,int b)<\/p>\n<p>{<br \/>\nreturn (a+b);<br \/>\n}<br \/>\nint sub (int a, int b)<br \/>\n{<br \/>\nreturn( a-b);<br \/>\n}<\/p>\n<p>Here is the header<\/p>\n<p>\/* a.h*\/<\/p>\n<p>int add(int,int);<br \/>\nint sub(int,int);<\/p>\n<p>Compile add.c to get add.o<\/p>\n<pre class=\"code\">gcc <a href=\"http:\/\/www.adp-gmbh.ch\/cpp\/gcc\/gcc_options.html#opt_c\">-c<\/a> add.c \r\n<\/pre>\n<p>You will have an object file add.o .<\/p>\n<p>Now\u00a0 create a static library\u00a0 with<\/p>\n<p>ar rcs\u00a0 libadd.a add.o<\/p>\n<p>If you have a program which uses functions from your new library as below<\/p>\n<p>\/*\u00a0 main.c *\/<\/p>\n<p>#include &#8220;a.h&#8221;<br \/>\nint main()<br \/>\n{<br \/>\nprintf (&#8220;%d\\n&#8221;, add(5,10));<br \/>\nprintf (&#8220;%d\\n&#8221;, sub(10,5));<br \/>\nreturn 0;<br \/>\n}<br \/>\nYou can link against\u00a0 our new\u00a0 library\u00a0 like this.<\/p>\n<p>gcc -o main main.c -L. -ladd<\/p>\n<p>( Note the -L. and -ladd. -L. say that look for library in current directory and -ladd says that the name of the library is libadd.a)<\/p>\n<p>For creating dynamic libraries, first compile the file containing library as below.<\/p>\n<pre class=\"code\">gcc <a href=\"http:\/\/www.adp-gmbh.ch\/cpp\/gcc\/gcc_options.html#opt_c\">-c<\/a> <a href=\"http:\/\/www.adp-gmbh.ch\/cpp\/gcc\/gcc_options.html#opt_f_cap_pic\">-fPIC<\/a>   add.c\r\n\r\nand\r\n\r\n ld -shared -soname libadd.so.1 -o libadd.so.1.0 -lc add.o\r\n\r\n ln -sf libadd.so.1 libadd.so<\/pre>\n<p>\/sbin\/ldconfig -v -n .<\/p>\n<p>( Dont forget .)<\/p>\n<p>Now you are ready to use the library<\/p>\n<p>gcc -o main\u00a0 main.c -L. -ladd<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an exercise I did\u00a0\u00a0 today. I\u00a0 am documenting it for my own reference. The problem was simple. Create a library with functions. add(int,int) and sub (int,int) and then use the library\u00a0 in another program. Here is the code \/*add.c*\/ #include &#8220;a.h&#8221; int add( int a,int b) { return (a+b); } int sub (int&hellip; <a class=\"more-link\" href=\"https:\/\/brainstorms.in\/?p=249\">Continue reading <span class=\"screen-reader-text\">How to create static and dynamic libraries  with gcc<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts\/249"}],"collection":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=249"}],"version-history":[{"count":0,"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions"}],"wp:attachment":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}