{"id":470,"date":"2017-07-14T09:58:34","date_gmt":"2017-07-14T09:58:34","guid":{"rendered":"http:\/\/perfectionatic.org\/?p=470"},"modified":"2020-06-02T11:19:13","modified_gmt":"2020-06-02T11:19:13","slug":"julia-calling-c-a-more-minimal-example","status":"publish","type":"post","link":"https:\/\/perfectionatic.org\/?p=470","title":{"rendered":"Julia calling C: A more  minimal example"},"content":{"rendered":"\n<p>Earlier I presented a <a href=\"http:\/\/perfectionatic.org\/?p=249\">minimal example<\/a> of <code>Julia<\/code> calling <code>C<\/code>. It mimics how one would go about writing <code>C<\/code> code, wrapping it a library and then calling it from Julia. Today I came across and even more minimal way of doing that while reading an <a href=\"https:\/\/julialang.org\/blog\/2017\/01\/moredots\">excellent blog<\/a> on Julia&#8217;s syntactic loop fusion. Associated with the blog was <a href=\"https:\/\/github.com\/JuliaLang\/julialang.github.com\/blob\/master\/blog\/_posts\/moredots\/More-Dots.ipynb\">notebook<\/a> that explores the matter further.<\/p>\n\n\n\n<p>Basically, you an write you <code>C<\/code> in a string and pass it directly to the compiler. It goes something like<\/p>\n\n\n\n<pre lang=\"julia\">using Libdl\nC_code= raw\"\"\"\n       double mean(double a, double b) {\n         return (a+b) \/ 2;\n       }\n       \"\"\"\nconst Clib=tempname()\nopen(`gcc -fPIC -O3 -xc -shared -o $(Clib * \".\" * Libdl.dlext) -`, \"w\") do f\n     print(f, C_code)\nend\n<\/pre>\n\n\n\n<p>The <code>tempname<\/code> function generate a unique temporary file path. On my Linux system <code>Clib<\/code> will be string like <code>\"\/tmp\/juliaivzRkT\"<\/code>. That path is used to generate a library name <code>\"\/tmp\/juliaivzRkT.so\"<\/code> which will then used in the <code>ccall<\/code>:<\/p>\n\n\n\n<pre lang=\"julia\">\nmeanc(a,b)=ccall((:mean,Clib),Float64,(Float64,Float64),a,b)\njulia> meanc(3,4)\n3.5\n<\/pre>\n\n\n\n<p>This approach would not be recommended if you are writing anything sophisticated in <code>C<\/code>. However, it is fun to experiment with for short bits of <code>C<\/code> code that you might like to call from Julia. Saves you the hassle of creating a <code>Makefile<\/code>, compiling, etc&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier I presented a minimal example of Julia calling C. It mimics how one would go about writing C code, wrapping it a library and then calling it from Julia. Today I came across and even more minimal way of doing that while reading an excellent blog on Julia&#8217;s syntactic loop fusion. Associated with the [&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-470","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\/470","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=470"}],"version-history":[{"count":13,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":692,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions\/692"}],"wp:attachment":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}