• Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created over 15 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dynamically load and use native C libs from Clojure using JNA

Dynamically load and use native C libs from Clojure using JNA

clojure-jna is available from clojars. Just add this to your Leiningen project.clj in the :dependencies section:

[net.n01se/clojure-jna "1.0.0"]

=== Usage

(require '[net.n01se.clojure-jna :as jna])
(jna/invoke Integer c/printf "My number: %d\n" 5)
; My number: 5
;=> 13

The first argument to jna-invoke is the native function's return value. The second is a symbol, in this case c/printf. The c part is the name of the library, in this case libc. The printf part is of course the name of the function to call. The rest are arguments to the native function.

That 13 is printfs return value -- I guess it's the number of bytes printed or something? Anyway, feel free to ignore it just like all C programs do.

If you're going to be calling the same function a few times, you might find it convenient to be able to call it like a regular Clojure function. Use jna-fn for that:

(doc jna/to-fn)
; -------------------------
; net.n01se.clojure-jna/to-fn
; ([return-type function-symbol])
; Macro
;   Return a Clojure function that wraps a native library function:
;    (def c-printf (jna/to-fn Integer c/printf))
;    (c-printf "My number: %d\n" 5)

(def c-printf (jna/to-fn Integer c/printf))

(c-printf "My number: %d\n" 5)
; My number: 5
;=> 13

(c-printf "My number: %d\n" 10)
; My number: 10
;=> 14

If you're going to be calling a bunch of functions from the same native lib, you might like to use jna-ns to create a Clojure namespace full of Clojure functions that wrap the native functions:

(doc jna/to-ns)
; -------------------------
; net.n01se.clojure-jna/to-ns
; ([new-ns libname fnspecs])
; Macro
;   Create a namespace full of Clojure functions that wrap functions from
;   a native library:
;   (jna/to-ns native-c c [Integer printf, Integer open, Integer close])
;   (native-c/printf "one %s two\n" "hello")

(jna/to-ns native-c c [Integer printf, Integer open, Integer close])
;=> #<Namespace native-c>

(native-c/printf "one %s two\n" "hello")
; one hello two
;=> 14

(native-c/open "README")
;=> -1

More Repositories

1

clojure-classes

Graph of Clojure classes and tools to produce it.
Clojure
50
star
2

tractionsvg

Presentations without slides
Clojure
24
star
3

finger-tree

2-3 finger trees for Clojure
Clojure
14
star
4

textjure

Programmer's text editor with integrated Clojure REPL (ultra-pre-alpha)
Clojure
13
star
5

spread

Macros for building Clojure maps using syntax like JavaScript's shorthand properties and spread syntax.
Clojure
10
star
6

JaM

Lisp-like macros for JavaScript
JavaScript
10
star
7

talk-cljs

Clojure
7
star
8

talk-finger-tree

Slides for (first clojure-conj) talk, "Finger Trees, Custom Persistent Collections"
JavaScript
7
star
9

clojure-compiler

unofficial experiments for a self-hosted clojure compiler
Clojure
6
star
10

tmux-share

Tools for sharing tmux sessions on a multi-user pairing host
Shell
6
star
11

wallframe-alpha

Find pleasing arrangement of pictures frames on dining room wall
HTML
6
star
12

chouser.github.io

Chouser's homepace (at github)
HTML
5
star
13

lisp-in-small-pieces-clj

Lisp In Small Pieces examples implemented in Clojure instead of Scheme
Clojure
4
star
14

Taskology

JavaScript
4
star
15

clojure-irc-log

Code used to convert IRSSI IRC logs to the html format shown at http://clojure-log.n01se.net/
Clojure
4
star
16

gryphonport

Clojure
2
star
17

clojure-talk-conc

Materials for a talk on Clojure Concurrency
Clojure
2
star
18

clojure-projtools

Tools for managing tickets and patches against clojure and contrib
Clojure
2
star
19

cljs-svc

Clojure
1
star
20

LiSP2022

Website and shared resources for collaborative learning of Lisp in Msall Pieces starting in 2022
CSS
1
star
21

conj2015

Code for talk at Clojure/conj 2015
Clojure
1
star
22

talk-expression-problem

Slides for Strange Loop 2010 talk, "Clojure's Solutions to the Expression Problem"
Clojure
1
star
23

tweak-aws-console

adjust AWS console UI in ways I find pleasing
CSS
1
star
24

apply-concat-and-friends

HTML
1
star
25

data-munge

Clojure
1
star
26

image-grid

Overlay a graphic with a grid of lines to fascilitate sketching.
1
star
27

netbible-chouser

Chrome extension to tweak the NetBible UI
JavaScript
1
star