{"id":1345,"date":"2018-02-20T17:02:52","date_gmt":"2018-02-20T16:02:52","guid":{"rendered":"https:\/\/mihosoft.eu\/?p=1345"},"modified":"2018-02-20T17:09:19","modified_gmt":"2018-02-20T16:09:19","slug":"running-c-code-from-java-via-vtcc","status":"publish","type":"post","link":"https:\/\/mihosoft.eu\/?p=1345","title":{"rendered":"Running C code from Java via VTCC"},"content":{"rendered":"<p>In one of our projects we transform C code via VMF &amp; VMF-Text which is a powerful language modeling framework based on ANTLR4 grammars. It can parse and write (&#8220;unparse&#8221;) code that conforms to a specified grammar and provides a Java API for manipulating the model. In one of our applications we use this infrastructure to add source-based instrumentations to a subset of C. <\/p>\n\n<p>The problem is running and testing C code without requiring a (manually installed) native Compiler. On macOS and Windows this can involve downloading several GB for Xcode and Visual Code. There are smaller alternatives. But even they require additional installation and path adjustments. For on-the-fly execution of C code this is overkill.<\/p>\n\n<p>To test and execute the C code hassle-free, I worked on a small Java library <a href=\"https:\/\/github.com\/miho\/VTCC\/\">VTCC<\/a> which uses the <a href=\"https:\/\/bellard.org\/tcc\/\">TCC<\/a> compiler to run C code directly from Java. To prevent the JVM from crashing the code runs in its own process (for now, JNI integration might follow at some point). <\/p>\n\n<p><a href=\"https:\/\/github.com\/miho\/VTCC\/\">VTCC<\/a> embedds a tiny C compiler (&lt; 1MB) to compile and run the C code snippets (works for Linux, macOS and Windows on x86 &amp; x64). The API is fairly simple. Here&#8217;s an example:<\/p>\n\n<div id=\"attachment_1370\" style=\"width: 710px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3.png\"><img aria-describedby=\"caption-attachment-1370\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3-1024x493.png\" alt=\"VTCC - run C code from Java\" width=\"700\" height=\"337\" class=\"size-large wp-image-1370\" srcset=\"https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3-1024x493.png 1024w, https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3-300x144.png 300w, https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3-768x370.png 768w, https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3-700x337.png 700w, https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3.png 1330w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><p id=\"caption-attachment-1370\" class=\"wp-caption-text\">VTCC &#8211; run C code from Java<\/p><\/div>\n\n<p>The code is hosted on GitHub: <a href=\"https:\/\/github.com\/miho\/VTCC\/\">https:\/\/github.com\/miho\/VTCC\/<\/a>. Binary distributions with an embedded compiler for Linux, macOS and Windows are available via bintray (maven central&amp;jcenter). For an out-of-the-box experience you need two dependencies, the VTCC library and the binary dependency. <\/p>\n\n<p><strong>VTCC:<\/strong> <\/p>\n\n<p><a href=\"https:\/\/bintray.com\/miho\/TCC\/VTCC\/_latestVersion\"> <img decoding=\"async\" src=\"https:\/\/api.bintray.com\/packages\/miho\/TCC\/VTCC\/images\/download.svg\" alt=\"Download VTCC\" \/> <\/a> <\/p>\n\n<p><strong>tcc-dist:<\/strong><\/p>\n\n<p><a href=\"https:\/\/bintray.com\/miho\/TCC\/tcc-dist\/_latestVersion\"> <img decoding=\"async\" src=\"https:\/\/api.bintray.com\/packages\/miho\/TCC\/tcc-dist\/images\/download.svg\" alt=\"Download tcc-dist\" \/> <\/a><\/p>\n\n<p><strong>Example (Gradle):<\/strong><\/p>\n\n<pre><code>\n  dependencies {\n      \/\/ core library\n      compile 'eu.mihosoft.vtcc:vtcc:2018.1.3'\n      \/\/ binary dependencies\n      compile 'eu.mihosoft.vtcc.tccdist:tcc-dist:2018.1.3'\n  }\n<\/code>\n<\/pre>\n\n<p>We are also going to add this library as plugin for VRL-Studio to finally add C support (for a subset of C).<\/p>\n\n<p>There are many features that could be implemented on top of this like JNI support or shared memory between the JVM and native processes. If you have ideas don&#8217;t hesitate to contact me!<\/p>\n\n<p>Stay tuned and <a href=\"https:\/\/twitter.com\/intent\/user?screen_name=mihosoft\">follow me on Twitter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of our projects we transform C code via VMF &amp; VMF-Text which is a powerful language modeling framework based on ANTLR4 grammars. It can parse and write (&#8220;unparse&#8221;) code that conforms to a specified grammar and provides a Java API for manipulating the model. In one of our &#8230;<\/p>","protected":false},"author":1,"featured_media":1370,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[146,13,129,145,5],"tags":[152,147,148,151,149,150,125,24],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/mihosoft.eu\/wp-content\/uploads\/2018\/02\/carbon3.png","jetpack_shortlink":"https:\/\/wp.me\/p2P2yR-lH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts\/1345"}],"collection":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1345"}],"version-history":[{"count":29,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":1377,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts\/1345\/revisions\/1377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/media\/1370"}],"wp:attachment":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}