Interop with C in Clojure
(load “native”)
netpyoung@gmail.com
April 2019
#1
Do you have any Questions?
- for keep time, to save each others time
- If you have questions, during presentation.
- message on slack #clojure-sg with page #numer.
- https://clojurians.slack.com/messages/clojure-sg
- ex) #10 - what is blabla?
- I will answer after presentation.
- so, I can polish this slide from your feedbacks.
#2
● EunPyoung Kim
● KOR -> JPN -> SGP
● Game Client Programmer. (since 2012.04 ~ )
● I like Lisp (not only clojure)
● I’m Lazy
○ because of that I like automatation (code generate also)
○ I like macro (but don't abuse)
● I like Emacs
○ Cursive
○ (Neo)Vim….?
● https://netpyoung.github.io/
#3
What I want & What I did.
● I want to trying use SDL library via clojure
● I made some examples for c binding.
● I want to share approach and experience.
#4
Problem?
● How to load library.
● Writing wrapper function.
● C structure layout for pointer access.
#5
Java & C Binding
● performance check
○ https://github.com/yulewei/java-ffi-demo
● JNI > JNR > JNA (Direct Mapping) > JNA (Interface Mapping)
name date
JNI 1997.02
JNA 2007.03
JNR 2013.02
JavaCPP 2015.07
Panama EAB
#6
No Examples on This
presentation
JNI
● from JDK 1.1
● https://docs.oracle.com/javase/8/docs/technotes/guides/jni/
#7
JNI Example
1. prepare native c library
2. writing && compile interop java file
3. generate interop c header
4. writing && compile interop c library
5. writing && build clojure project with interop java file
6. call clojure library with native c library and interop c library
#8
JNA
● https://github.com/java-native-access/jna
#9
JNA Example
1. prepare native c librar
2. writing && build clojure project
#10
JNR
● https://github.com/jnr/jnr-ffi
○ jnr-ffi is a Java library for loading native libraries without writing JNI code by hand, or using
tools such as SWIG.
● https://www.slideshare.net/skrb/jnr-java-native-runtime
● https://www.oracle.com/technetwork/java/jvmls2013nutter-2013526.pdf
#11
JNR Example
1. prepare native c librar
2. writing && build clojure project
#12
JavaCPP
● https://github.com/bytedeco/javacpp
#13
JavaCPP Example
● https://github.com/bytedeco/javacpp/wiki/Examples
#14
Panama
● https://openjdk.java.net/projects/panama/
● Panama: A Foreign Policy for Java by Maurizio Cimadamore
● Going Meta to Valhalla & Panama with John Rose
#15
Panama Example
● https://hg.openjdk.java.net/panama/dev/raw-
file/4810a7de75cb/doc/panama_foreign.html#jextract-a-jar-file-for-sqlite3.h
#16
SDL
● Simple DirectMedia Layer
● cross-platform development library.
● designed to provide low level access to audio, keyboard, mouse, joystick, and
graphics hardware via OpenGL and Direct3D
#17
SDL Language Bindings
● https://www.libsdl.org/languages.php
● Ada
● C#
● D
● Go
● Lua
● OCaml
● Pascal
● Python
● Rust
#18
Java SDL Binding?
● https://github.com/javasdl/libjavasdl
● using JNA
#19
JNA Example(SDL)
● SDL2(C language) binding in clojure
#20
Demo Clojure binding for SDL2
● https://github.com/netpyoung/cljpyoung.sdl2
#21
Questions
ex) #10 - what is blabla?
#22
Resources
● https://github.com/netpyoung/pt.c-interop-in-clojure
#23
Thank you

Interop with c in clojure

Editor's Notes

  • #25 Today is maybe my last day to attend this seminar