开发者

MinGW's compiler option Wl,--kill-at does not work

I am currently struggling to compile a Dll for JNI use, using Eclipse CDT and MinGW.

Following a tutorial, i created a Java class that declares native methods, then used javah to get the relevant header file and I implemented it in a C++ class.

The C++ code is very simple and compilation works, but when I load the library into the Java class, i get this error :

Exception in thread "main" java.lang.UnsatisfiedL开发者_StackOverflow社区inkError: Main.integerMethod(I)I
    at Main.integerMethod(Native Method)
    at Main.main(Main.java:12)

I "explored" the dll and found out that the methods that should be called all have a suffix like "@14". The problem is, I am already using the -Wl,--kill-at compiler option which should remove these very embarassing tags... So why is it not working ?

The compilation log is following :

**** Rebuild of configuration DLL for project JniCTest ****

**** Internal Builder is used for build               ****
g++ -IC:\Program Files\Java\jdk1.6.0_13\include -IC:\Program Files\Java\jdk1.6.0_13\include\win32 -O3 -Wall -c -fmessage-length=0 -mno-cygwin -D__int64=long long -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -oMain.o ..\Main.cpp
g++ -o libJniCTest.dll -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -shared -olibJniCTest.dll Main.o
Build complete for project JniCTest
Time consumed: 375  ms.  

Is there something wrong about the compiler options ? Thanks for any help.


Solution found. The --kill-at option was not put in the right command. Indeed MinGW first compiles the files into an .o object file, then (second line) it does the linking from this .o file. The option must therefore be placed into the second line.

Corrected commands for a source file Main.cpp and an output DLL libJniCTest.dll :

g++ -I"C:\Program Files\Java\jdk1.6.0_13\include" -I"C:\Program Files\Java\jdk1.6.0_13\include\win32" -O0 -Wall -c -oMain.o ..\Main.cpp
g++ -Wl,--kill-at -shared -olibJniCTest.dll Main.o


Also, don't forget to wrap your implementation like this

extern "C" {

//implemented methods

}

That took me hours to figure out


I followed this simple JNI tutorial on IBM's site and compiled the given Sample1.c file with the following command on Windows XP. Its working fine for me.

gcc -Wall -Wl,--kill-at -shared Sample1.c -o Sample1.dll -I"C:\Program Files\Java\jdk1.7.0\include" -I"C:\Program Files\Java\jdk1.7.0\include\win32"

P.S: Change the JDK path as per your system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜