Tuesday, April 24, 2012

Fixing Unsatisfiedlinkerror - Can't Find Dependent Libraries

After I finally got my Java JNI code working on my Windows development machine that sets the local time of the computer from Java, I discovered it was not working on the production machine. I got the dreaded Unsatisfiedlinkerror - Can't Find Dependent Libraries runtime error. Following is an explanation of why it was happening and what I did to fix it...

The Cause - Visual Studio C++ 2010 Express

In order to find what dependent library was missing on the production machine that was present on my development machine, I downloaded and installed Dependency Walker. After analyzing my generated DLL I saw something unexpected.

What the heck is MSVCR100.DLL?? It turns out it's added by Visual Studio C++ during the build of the DLL, and it is NOT necessary.

The Solution

There seem to be several solutions to this problem, but the most elegant in my opinion to to configure Visual Studio to not make the built DLL dependent on it in the first place. Here's how to do that: Right click on the DLL project and open up the properties dialog. Under Configuration Properties -> C/C++ -> Code Generation -> Runtime Library select "Multi-threaded (/MT)". source

Rebuild your DLL and verify with Dependency Walker that the rogue dependency is gone.

Piece of Cake!!!

1 comment:

Anonymous said...

The dependency is "gone" in the sense that you are now statically linking the CRT to your code.

In other words, you will now be unable to link your JNI DLL to any third party library that does not use the exact same version of the CRT you linked to.

In other words, this solution will only work on small personal projects with no outside dependencies. By linking statically, you simply masked the problem.

Also note that it is possible you did not need the CRT but unlikely.