{"id":249,"date":"2016-09-25T19:41:05","date_gmt":"2016-09-25T19:41:05","guid":{"rendered":"http:\/\/perfectionatic.org\/?p=249"},"modified":"2017-03-28T20:50:35","modified_gmt":"2017-03-28T20:50:35","slug":"julia-calling-c-a-minimal-example","status":"publish","type":"post","link":"https:\/\/perfectionatic.org\/?p=249","title":{"rendered":"Julia calling C: A minimal example"},"content":{"rendered":"<p>This blog is a &#8220;Hello World&#8221; example of Julia calling C.<\/p>\n<p>We start of by at bit of C code we want to call from Julia. We write the following in <code>calc_mean.c<\/code><\/p>\n<pre lang=\"c\">\ndouble mean(double a, double b) {\n  return (a+b) \/ 2;\n}\n<\/pre>\n<p>To build the library, we need to create a <code>Makefile<\/code><\/p>\n<pre lang=\"make\">\nCC=gcc \n\nCFLAGS=-c -Wall -fPIC\n\nSOURCES=calc_mean.c \nOBJECTS=$(SOURCES:.c=.o)\n\n.c.o:\n    $(CC) $(CFLAGS) $< -o $@ \n\nlib: $(OBJECTS)\n    $(CC) -shared -fPIC -o libmean.so $(OBJECTS)\n\nclean:\n    rm *.o *.so\n\n<\/pre>\n<p>The option <code>fPIC<\/code> and <code>-shared<\/code> are essential for Julia to be able to resolve the function in our library. Now we are almost ready to build our library. From the <code>bash<\/code> terminal we invoke:<\/p>\n<pre lang=\"bash\">\nmake lib\n<\/pre>\n<p>This will generate a <code>libmean.so<\/code> file.<\/p>\n<p>In Julia we call the function in our c library by<\/p>\n<pre lang=\"julia\">\nx=ccall((:mean,\"libmean\"),Float64,(Float64,Float64),2.0,5.0)\nprintln(x)\n3.5\n<\/pre>\n<p>For this to work,<\/p>\n<ul>\n<li><code>Julia<\/code> must be running either on the same path where <code>libmean.so<\/code> resides, <\/li>\n<li>the path to <code>libmean.so<\/code> is in <code>LD_LIBRARY_PATH<\/code>, or <\/li>\n<li>the path to the library is <code>push<\/code>ed to <code>Libdl.DL_LOAD_PATH<\/code> via <\/li>\n<\/ul>\n<p>...<\/p>\n<pre lang=\"julia\">\npush!(Libdl.DL_LOAD_PATH,\"path_to_libmean.so\")\n<\/pre>\n<p>P.S. Thanks to <a href=\"http:\/\/www.stochasticlifestyle.com\/wordpress-julia-syntax-highlighting\/\">Christopher Rackauckas<\/a> for tips on <code>Julia<\/code> highlighting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog is a &#8220;Hello World&#8221; example of Julia calling C. We start of by at bit of C code we want to call from Julia. We write the following in calc_mean.c double mean(double a, double b) { return (a+b) \/ 2; } To build the library, we need to create a Makefile CC=gcc CFLAGS=-c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,74,73,71],"tags":[92],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-c","category-julia","category-julialang","category-programming","tag-julia"],"_links":{"self":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=249"}],"version-history":[{"count":21,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/249\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}