unsatisfied link error with 64-bit library generated by mingw64 toolchain

124 views
Skip to first unread message

wjohnson

unread,
Jan 26, 2012, 7:57:06 PM1/26/12
to Java Native Access
Environment: W7; amd64; NB 7.1; TDM-MinGW64 toolchain

I am just getting started with JNA. The interface and direct mappings
in the tutorial work with native libraries provided by MS, but I am
getting the UnsatisfiedLinkError when I substitute my g++ generated
dll library. The FAQ says that if functions have a suffix of '@NN',
pass a StdCallFunctionMapper. Mine do not. (See details below.)

I am at a loss to undertand what is failing and what I should do
differently.
Is there a way to generate more debugging messages?
Beyond the github documentation, what could I read to become better
informed of how the JNA glue works?

Best regards,

- wjohnson



Java source:

public class MyApi {

// Interfaces
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("msvcrt",
CLibrary.class);
void printf(String format, Object... args);
}

public interface MyLibrary extends Library {
MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary("jnademo",
MyLibrary.class);
int myfnc();
}

// Direct Mapping
static {
Native.register("msvcrt");
// Native.register("jnademo");
}

public static native double cos(double x);
// public static native int myfnc();

public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello world\n");
MyLibrary.INSTANCE.myfnc();

// System.out.println("cos: " + cos(0));
// System.out.println("myfnc: " + myfnc());
}
}

---
C++ source:

#include <iostream>
#include <jnademo.h>

using std::cout;
using std::endl;

int myfnc() {
int x = 50;
cout << "native_myfnc:" << x << endl;
return x;
}

---
Console output:
run:
Hello world

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error
looking up function 'myfnc': The specified procedure could not be
found.
at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:350)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:330)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at $Proxy1.myfnc(Unknown Source)
at jnademojava.MyApi.main(MyApi.java:40)
Java Result: 1

---
objdump -x jnademo.o

...
SYMBOL TABLE
[ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x0000000000000000
jnademo.cpp
File
[ 2](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x0000000000000000
_ZStL8__ioinit
[ 3](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x0000000000000000
_Z5myfncv
AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0
[ 5](sec 1)(fl 0x00)(ty 20)(scl 3) (nx 0) 0x0000000000000045
__tcf_0
[ 6](sec 1)(fl 0x00)(ty 20)(scl 3) (nx 0) 0x0000000000000060
_Z41__static_initialization_and_de
struction_0ii
[ 7](sec 1)(fl 0x00)(ty 20)(scl 3) (nx 0) 0x000000000000009c
_GLOBAL__sub_I__Z5myfncv
[ 8](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1)
0x0000000000000000 .text
...

Timothy Wall

unread,
Jan 26, 2012, 8:55:29 PM1/26/12
to jna-...@googlegroups.com
use extern "C" to avoid mangling function names, or use the mangled names.

wjohnson

unread,
Jan 26, 2012, 10:10:52 PM1/26/12
to Java Native Access
It worked. Thanks.
Reply all
Reply to author
Forward
0 new messages