Statically linking a Fortran .lib into a C++ .dll 
Author Message
 Statically linking a Fortran .lib into a C++ .dll

I am creating a C++ wrapper for a fortran function so I can use the
Fortran code with Java through JNI.

What I have been doing is compiling the Fortran code as a DLL
(fortran_code.dll).  I then link this with the C++ code, which is in
turn compiled as a DLL (C_code.dll).  The C++ code uses JNI to allow
Java to call the C++ function, which in turn calls the Fortran
function.  This works beautifully, except for the fact that C_code.dll
must be in the Java library search path, AND fortran_code.dll must be
in the Windows system library search path (c:\windows\system32).  This
is a pain for installation as the libraries must be copied into two
different locations.

Ideally, I would like the Fortran code statically linked into the C++
DLL.  So all I did was create a new Fortran project and specify it as
a Static Library.  I then linked the resulting file (fortran_code.lib)
to the C++ code, and simply changed the Fortran function declaration
in the C++ code to remove __declspec(dllimport).  The code compiled
fine, creating a larger c_code.dll file which includes the Fortran
code statically linked.  However, when Java now tries to load the new
DLL, it gives the following error:

java.lang.UnsatisfiedLinkError: <path>\c_code.dll: A dynamic link
library (DLL) initialization routine failed.

Does anybody have any idea what is causing this?  I tried dumpbin
/IMPORTS c_code.dll and there are none except kernel32.dll, which is
correct.  Statically linking in the Fortran code must have done
something to mess up a DLL which had been working when loading the
Fortran code from a DLL, but I am clueless as to why or what is
causing this error.

I also tried to debug this C++ wrapper by
writing a simple C++ program which calls the function so I could debug it
easier.  I then use this executable for the debug executable in the DLL
project, I get an error message: "The application failed to initialize
properly (0xc0000142).  Click on OK to terminate the application."  I then
click on OK, and it says "Unhandled exception at 0x77f9e4b4 in testapp.exe:
0xC0000142: DLL Initialization Failed."

Any ideas as to why the Fortran will work as it's own .dll, but not as a
.lib statically linked into the C++ .dll would be greatly appreciated.
Thank you.

Thu, 22 Sep 2005 06:44:10 GMT  
 Statically linking a Fortran .lib into a C++ .dll

>Any ideas as to why the Fortran will work as it's own .dll, but not as a
>.lib statically linked into the C++ .dll would be greatly appreciated.

You don't say what compilers you are using.  In general, static linking of routines from different languages into a single module (DLL or EXE), is highly compiler dependent.  If you want to do this, you must use compilers that are compatible.  Examples:
VC4 and FPS4, VC5 and DVF 5, VC6 and CVF6, Watcom C/C++ and F77 of the same version and sub-version, Absoft Fortran with Absoft C/C++, G77 with GCC or GPP -- but don't expect G77 generated .OBJs to statically link with OBJs from VC4.  Making that work may
be impossible, but it will be highly difficult and arcane at best.

Sun, 25 Sep 2005 02:20:00 GMT  
 Statically linking a Fortran .lib into a C++ .dll
I'm using CVF 6.0 and VC++ .NET.

Sun, 25 Sep 2005 04:29:06 GMT  
 Statically linking a Fortran .lib into a C++ .dll

>I'm using CVF 6.0 and VC++ .NET.

OK the upgrade to 6.1 is IIRC free, while possibly not critical, that is probably a very good idea.

CVF and VC++ should be compatible, but VC++ .NET is VC++ what? 7.0? 6.5?  You may need to upgrade to CVF 6.6 to statically link with .NET C/C++ objects.  The Compaq help forum would be a good place to ask also.

Also be aware that mixing i/o is problematic, the Fortran run-time and C/C++ run-times are separate.  If this may be an issue, bring it up in a new thread.

Next, for statically linking Fortran and C/C++ into a single module, start by ALSO statically linking the run-time libraries into the whole mess -- the code will likely bloat badly -- but see if the error changes or goes away.  In this case that means,
for a DLL target, DFORDLL.LIB and LIBC.LIB -- I think.  It may not be possible to force static linking or the run-time when targeting a DLL these days, but it is worth a try.

Good luck.


Mon, 26 Sep 2005 01:31:50 GMT  
 Statically linking a Fortran .lib into a C++ .dll
Thank you for your help.

I figured out that the problem is that the C Run-Time (which the Fortran
Run-Time is dependent upon) in DVF 6.0 is not compatible with VC7.  I had a
co-worker compile the code on CVF 6.6 for me and then plugged this LIB file
into my VC7 project and all worked beautifully.  Hopefully I will now have
some leverage to request a copy of CVF 6.6 for my project. :)

By the way, I was statically linking the (multi-threaded) runtimes into my
DLL.  The files are DFORMT.LIB and LIBCMT.LIB.  This works fine whther you
are building an EXE, DLL or whatever and is definitely what I need as I am
developing a Java interface to Fortran code and want as much portability as
possible (the next task is to make a shared object for Linux).

Thank you again for your help.  There is a long-running thread on thsi topic
at the Intel Fortran Developer Forum if you or anybody else trying to do
something similar has any interest in checking it out.  There is much more
information on the hurdles which you may run into trying to create a DLL
which contains object code for C++ and Fortran.  Here is the link:

(Be sure to copy the entire link in case it doesn't fit onto one line.)

Good luck.  I finally got mine to work, so I am happy. :) Haha.

Mon, 26 Sep 2005 10:40:30 GMT  
 [ 5 post ] 

 Relevant Pages 

1. Statically Linking tcl.lib and tk.lib into a standalone .exe file

2. Statically linking Fortran code to Visual C++

3. How to link statically with python15.lib....

4. Trouble linking FORTRAN DLL with C++ application (undefined reference)

5. Unresolved External error when linking a Fortran Dll from C++

6. Statically link Python15.LIB instead of Python15.lib -> Python15.dll

7. Statically link Fortran lib to Delphi app instead of using DLL?

8. how to statically link against tcl84.lib, tk84.lib, tclx84.lib?

9. statically linked tcl conflict with dynamically linked tcl error

10. Linking FORTRAN Routine to C++ Program, MS C++ DVF Library Problem

11. Linking Watcom Obj/Lib/Dll files in Lahey LF40

12. Errors trying to link in .obj or .lib from Visual C++


Powered by phpBB® Forum Software