using swig in clojure

201 views
Skip to first unread message

Seth

unread,
Nov 4, 2010, 6:42:22 PM11/4/10
to Clojure
Ive recently had troubles using swig in clojure getting a 'unsatisfied
link exception' even though using the swig generated library worked in
regular java code. I believe there was a post on this somewhere in
these google groups.

Anyways, I have figured out that if I place the following code in a
clojure file (test.clj)
(System/loadLibrary "Seth")

and go (compile 'test) on the REPL, i get


No such file or directory
[Thrown class java.io.IOException]

Restarts:
0: [QUIT] Quit to the SLIME top level

Backtrace:
0: java.io.UnixFileSystem.createFileExclusively(Native Method)
1: java.io.File.createNewFile(File.java:900)
2: clojure.lang.Compiler.writeClassFile(Compiler.java:5885)
3: clojure.lang.Compiler.compile(Compiler.java:6043)
4: clojure.lang.RT.compile(RT.java:368)
5: clojure.lang.RT.load(RT.java:407)
6: clojure.lang.RT.load(RT.java:381)
7: clojure.core$load$fn__4511.invoke(core.clj:4905)
8: clojure.core$load.doInvoke(core.clj:4904)
9: clojure.lang.RestFn.invoke(RestFn.java:409)
--more--




However, afterwards i can succesfully do
(import Seth)
(Seth/add 2 3) => 5

I cant do the loadlibrary thing on the repl, or it wont work (i get
the 'unsatisfied link error' when calling (Seth/add)). Notice that if
I do (compile 'test) again i get the same error above, which is
weird because if i do (System/loadLibrary "Seth") on the repl i get
the 'expected' error

Native Library /home/seth/.random/java/libSeth.so already loaded in
another classloader
[Thrown class java.lang.UnsatisfiedLinkError]

Restarts:
0: [QUIT] Quit to the SLIME top level

Backtrace:
0: java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1715)
1: java.lang.ClassLoader.loadLibrary(ClassLoader.java:1675)
2: java.lang.Runtime.loadLibrary0(Runtime.java:840)
3: java.lang.System.loadLibrary(System.java:1047)
4: user$eval1825.invoke(NO_SOURCE_FILE:1)
5: clojure.lang.Compiler.eval(Compiler.java:5424)
6: clojure.lang.Compiler.eval(Compiler.java:5391)
7: clojure.core$eval.invoke(core.clj:2382)
--more--




Anyone know whats going on and how this can be fixed?

mac

unread,
Nov 5, 2010, 11:54:29 AM11/5/10
to Clojure
System/loadLibrary uses the paths set in the System property
java.library.path to look for dynamic libraries so you need to make
sure it contains the directory where your .so is. I think it also gets
cached at first read or something stupid like that so it's very
important to get java.library.path right before the first time you
call System/loadLibrary. The syntax for java.library.path is the same
as for classpath (wich is the same as for the OS path env. variable).
You also have to make sure not to call System/loadLibrary from the top
level of a file unless you are just playing with the repl. You need to
call it from inside a function or everything will break if you try to
pre compile the code since the library will be linked at compile time
only and not at runtime... So to make sure it's linked at runtime,
never put System/loadLibrary on the top level.

To set java.library.path from the command line use the parameter -
Djava.library.path='your stuff here'. To set it from Clojure code:
(System/setProperty "java.library.path" "your stuff here")
/Markus

Seth

unread,
Nov 19, 2010, 9:44:43 AM11/19/10
to Clojure
unfortunately doesnt work. The library loads succesfully but i still
get the error when calling add. Note that compiling on the top is a
workaround to get it working on the repl.

i added the loadlibrary to an init function which is good and i
decided to ahead of time compile it - and it worked! I could call
(init) and then add, without getting a link error!

The question is why do i need to ahead of time compile this code to
get it to work? Any ideas?

mac

unread,
Nov 26, 2010, 4:26:19 AM11/26/10
to Clojure
Without looking at your code I can't really say what is going wrong. I
can only repeat what I said before about System/loadLibrary requiring
the dynamic C library to be in the java.library.path path, which must
be set before calling System/loadLibrary for the first time. That's
the only issue I can remember having with it right now.
If the library seems to load fine but you get a link error when
looking up a function then the problem could be that you are loading
the wrong version of the library, or you just spelled something wrong?

Reply all
Reply to author
Forward
0 new messages