Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29246 Discussions

Linking Fortran Library into C++ code - Microsoft compiler/linker

Wesley_M_
Beginner
1,708 Views
Hi. I am running into some trouble linking a library in fortran with a C++ code in Windows. I am generating a .dll file using Intel Visual Fortran 11.1.051. Immediately after the declaration of the subroutine that I am using in the C++ code, I added "!DEC$ ATTRIBUTES DLLEXPORT :: UMAT" (UMAT is the name of the subroutine) and was able to build a .dll file and import library. I brought these over to my C++ code and managed to get everything to link nicely. Note that my C++ code is using Microsoft's compiler in VS2010. Both the .dll file that I made as well as the C++ project use the multi-threaded dll runtime library (at least I am pretty sure). I am also linking ifconsol.lib into my C++ code. Also, everything is in 64 bit...
Now, when I run the code, it crashes when it hits the call to the function that I wrote in Fortran. No messages or anything. I am guessing I'm just missing something.
A few possibilities of things I might be messing up -
  • If there are multiple subroutines in the fortran code that are used by the one that I am exporting, do I have to export those subroutines to the .dll as well?
  • Is there some fundamental incompatability regarding Intel Visual Fortran and Microsoft's C++ compiler/linker?
I'm not really a developer, so I would not be at all surprised if there was something really simple that I was missing...
0 Kudos
7 Replies
JVanB
Valued Contributor II
1,708 Views
Given that you didn't post the C++ prototype for subroutine UMAT nor the specification part of the Fortran subroutine UMAT, it seems likely that you don't fully appreciate the importance of getting these to match exactly, and that is dollars to donuts the reason for the crash on subroutine invocation.
0 Kudos
bmchenry
New Contributor II
1,708 Views
You mention you use VS2010 for the C++. Do you also use VS2010 for Fortran? Since if it is being compiled in VS 2008 that would cause compatibility issues (different C++ linkings i do believe)
There shouldn't be any incompatibility between C++ and Fortran unless you are using different VS versions
I'd also suggest a quick way to see what might be the issue is to use something like dependancy walker to check what your fortran DLLs need.
http://www.dependencywalker.com/

0 Kudos
Wesley_M_
Beginner
1,708 Views
Hmm. So I cannot link a dll created using Intel Visual Fortran 11.1 in VS2008 into a C++ code made using VS2010. Bummer. I tried setting the VS2010 project to use the v90 platform toolset (which I think means that it will use the compiler and libraries, etc. from VS2008) and that did not fix things.
Since it will not be convenient to move my code back to VS2008, I figure I should look at upgrading the Fortran tools I have from Intel, so my question is changing a bit now...
If I get the newest version of Intel's Fortran compiler, will it allow me to build a .dll that I can link against a C++ code that I make using Microsoft's toolset in VS2010, or will I have to get the Intel C++ compiler as well and build my C++ code with that?
I'd appreciate hearing from an Intel representative on this one, since it involves potential expenditures to acquire updated Intel products.
0 Kudos
bmchenry
New Contributor II
1,708 Views
I'm not with INTEL...but it is Fried day afternoon (and by Fried i mean Fried! Hot Hot Hot!) so they may be off running about trying to stay cool.
anyhow...If you alrady have C++ in VS2010, then you should be able to simply install Intel Fortran to VS2010??!! Simply rerun the installation and the 'do you want to install to VS2010' will come up in additional to VS2008)
Or if you using the shell of Intel Fortran (meaning in DEMO mode)?? then simpy geting Intel Fortran license will allow you to install it to whatever VS you have (VS2008 or VS2010)

0 Kudos
Steven_L_Intel1
Employee
1,708 Views
Ross, you say you "cannot link a dll", but have not shown what goes wrong or explained how you tried to do it. You can do this. Really, all you need to do is add the export library (.lib) created when the Fortran DLL builds to your C++ project. It is no problem to do this in VS2010 for a DLL built using VS2008.

I will mention one important thing. Microsoft made a change in VS2010 so that it will not link in libraries created by non-C++ dependent projects. You must explicitly reference the .lib from the other project either as a "source file" or under Linker > Input > Additional Dependencies.

And a second Microsoft bug that I will mention for completeness - A Fortran project dependent on a C++ DLL project won't link in the C++ DLL's export library. A static library works. Again, in this case you have to explicitly reference the DLL project's export library. And yes, this is a Microsoft bug even though it's the Fortran project doing the link.
0 Kudos
Wesley_M_
Beginner
1,708 Views
Steve,
Thank you for your reply. Allow me to elaborate some.
I am creating a .dll using a fortran code with Intel Visual Fortran 11.1.051 in Microsoft Visual Studio 2008. Just to give a little bit of a better idea of what I am doing, it is a custom Abaqus UMAT that I am hoping to use in an in-house C++ finite element code. The fortran code looks as follows:
[fxfortran] SUBROUTINE UMAT(VNAMES) !DEC$ ATTRIBUTES DLLEXPORT :: UMAT[/fxfortran] I have obviously shortened the variable list. Basically, it is a single subroutine in the fortran file that I want to use in C++ (although there are other subroutines which are used by the main subroutine - I am assuming I don't have to add the compiler directive to export those to access the main UMAT subroutine). In the project settings, under the Fortran->Libraries option, I have selected the Multithread DLL runtime library. I am building for the x64 platform and a release configuration.

On the C++ side of things, I have a big, multi-project C++ finite element code that is set up in VS2010. I am calling the UMAT function at one place in the code. It is prototyped as follows:
[cpp]extern "C" { void UMAT(double * VNames); }[/cpp]
I am linking the umat library's import library into the main C++ code in the main project's properties in Linker->Input->Additional Dependencies. I have also linked in ifconsol.lib. The project is building for x64, release. I am using the Multithreaded DLL Runtime Library, and the v100 toolset. Building and linking works fine; there are no unresolved externs and I get a runable executable.

I have added the directory containing the UMAT dll file to the system path environment variable. When I execute the code, it runs right up to the point that the UMAT gets called, and then crashes with a windows "such and such program has stopped working" message that gives me the "check online", "close program", and "Debug the Program" options, of which "close program" is the only option that accomplishes anything. The one useful piece of information that I can glean from the crash dialog is that the "Fault Module Name" is the dll file containing the UMAT.

I have tried compiling and running the C++ code on two different machines - one of which is the same machine that I am using to create the dll file. Both crash in exactly the same way.

Hopefully this provides some helpful information that might suggest where I am going wrong.

-Ross
0 Kudos
Steven_L_Intel1
Employee
1,708 Views
Ross,

You have not provided enough information to help you. Paraphrases and snippets of code can be misleading. For example, I assume that your Fortran code actually has "!DEC$ ATTRIBUTES DLLEXPORT :: UMAT". But I don't know how you declared VNAMES, and more important, I don't know what compiler options you used. I don't know what the C++ code that calls UMAT looks like.

Since you have a small test case you are working with, please ZIP the VS2008 solution (do a Build > Clean first) and ZIP the VS2010 C++ project (also clean it first) and attach these to a reply here (see my signature for instructions.) Then I will know exactly what you are doing and I can help set you on the right path.
0 Kudos
Reply