<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged ffi</title>
<link>https://ask.clojure.org/index.php/tag/ffi</link>
<description></description>
<item>
<title>How to invoke native C-functions in Clojure via JNR-FFI?</title>
<link>https://ask.clojure.org/index.php/9915/how-to-invoke-native-c-functions-in-clojure-via-jnr-ffi</link>
<description>&lt;p&gt;Hi Everyone,&lt;/p&gt;
&lt;p&gt;I want to call native functions in a C-Library from Clojure via &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jnr/jnr-ffi&quot;&gt;JNR-FFI&lt;/a&gt; without having to write Java code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defproject mypkg
	:dependencies [[org.clojure/clojure &quot;1.10.1&quot;]
				   [com.github.jnr/jnr-ffi &quot;2.2.0&quot;]])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on the jnr-ffi examples &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jnr/jnr-ffi-examples/blob/master/getpid/src/main/java/getpid/Getpid.java&quot;&gt;here&lt;/a&gt; and &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.programcreek.com/java-api-examples/?api=jnr.ffi.LibraryLoader&quot;&gt;here&lt;/a&gt; a Java wrapper might look like&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;package blahblah;

public class BlahNative {
	private static int[] intDummy;
	private static double[] doubleDummy;

	public BlahNative() {
	}

	public static native void native_a(char var0, char var1, int var2, int var3, int var4, double[] var7, int var8, int var9);

	public static native void native_b(char var0, char var1, int var2, int var3, int var4, double[] var7, int var8, int var9);

	static {
		lib = LibraryLoader
            .create(LibSodium.class)
            .search(&quot;/usr/local/lib&quot;)
            .search(&quot;/opt/local/lib&quot;)
            .search(&quot;/usr/lib&quot;)
            .search(&quot;/lib&quot;)
            .load(LIBRARY_NAME);
        initializeLibrary(lib);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I presume my Clojure namespace should use the &lt;a rel=&quot;nofollow&quot; href=&quot;http://clojure-doc.org/articles/language/interop.html&quot;&gt;gen-class&lt;/a&gt; feature for implementing Java classes.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns mypkg.native
	(:import [jnr.ffi LibraryLoader])
	(:import [jnr.ffi.annotations IgnoreError])
	(:import [jnr.ffi.provider FFIProvider])
    (:gen-class))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;How do I write a &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.baeldung.com/java-native&quot;&gt;native&lt;/a&gt; method in Clojure? In other words, do I even need to specify the method type (as &lt;code&gt;native&lt;/code&gt;) when using Clojure's Java interop features?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After loading the native library I do not know how to access the C-functions in the library. How do I do it?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To avoid reloading the library for every Clojure wrapper function of each C-functions, can I write a method like&lt;/p&gt;
&lt;p&gt; &lt;code&gt;(defn lib (. LibraryLoader load LIBRARY_NAME))&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;and then access the native functions in it?&lt;/p&gt;
&lt;p&gt;I am neither a C nor a Java expert so any guidance will be much appreciated.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
</description>
<category>Java Interop</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9915/how-to-invoke-native-c-functions-in-clojure-via-jnr-ffi</guid>
<pubDate>Fri, 04 Dec 2020 13:57:56 +0000</pubDate>
</item>
</channel>
</rss>