Background
I am trying to build a Java program that make use of an existing native library via JNI. There are two components of the native project that each build a shared library:
- A core component that builds
core.libandcore.dll - An application component that depends on the core component, and builds
application.libandapplication.dll
Using SWIG, I’ve created an application_wrap.c file, and corresponding Java files so that I can use JNI to access the native library.
What I would like to do is build a new shared library (lets call it application_jni.dll) containing this application_wrap.c file, so that I can load it in my Java program like this:
System.loadLibrary("application_jni");
Problem
Unfortunately, I can’t seem to figure out how to do this on Windows using the Visual Studio compiler. Here’s what I’ve tried:
First, compiling the application_wrap.c file generated by SWIG. This produces application_wrap.obj, and seems to work just fine.
cl /I "path/to/core/headers" /I "path/to/application/headers" -c application_wrap.c
Next, linking the application_wrap.obj against the existing shared libraries to create a new shared library:
link /dll /out:application_jni.dll application_wrap.obj core.lib application.lib
This results in a large number of errors that look like this:
application_wrap.obj : error LNK2019: unresolved external symbol __imp__function_in_core referenced in function_in_application_wrap
Any idea what could be causing these errors? I don’t have a lot of experience with compiling on Windows, so I wouldn’t be the least bit surprised to find that I’m missing some flags in the compile or link stages (or even misunderstanding how dll’s work).
A Few Extra Notes
Running
dumpbin /exports core.lib
shows that all of the functions mentioned by the error messages are in fact exported by the core shared library.
Edit
At Pavel’s suggestion I wrote a simple C program to see if it would link. I used the same compile and link commands as above, and I ran into the same error.
The C program test.c:
#include "application.h"
int main() {
function_in_application();
}
The error:
test.obj : error LNK2019: unresolved external symbol __imp__function_in_application referenced in function _main
The libraries failed to link because
core.dllandapplication.dllwere compiled for 64 bit, and the default target forcl.exeis 32 bit. The linker failed because i was trying to link a 32 bitobjfile to two 64 bitdll's. Compiling everything for 64 bit solved the problem.A Few Things To Be Aware Of
cl.exetarget 64 bit by running the thevcvarsall.batscript with an argument corresponding to your target platform (e.g. “amd64”). More in depth instructions can be found here.Visual Studio Express 2010does not install the 64 bit compilers by default. You may need to runsetup.exeagain and check the appropriate custom install options to install them.Visual Studio Express 2012does come with 64 bit compilers. I uninstalled the 2010 version, installed 2012, and had no problems after that.