Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Linking Fortran Static Libraries with C++, Argument Passing

polakse
Beginner
4,589 Views
First, a disclaimer: I know very little about Fortran and C++

I have some Fortran code that I have compiled into a multi-threaded static library. I want to call a subroutine in that Fortran code from a C++ "wrapper" application (DLL). I have been able to compile and link the Fortran and C++ code, however, when I pass arguments to the Fortran subroutine, they do not appear to be passed by reference. Here is some test code I am working with:

Fortran:
    SUBROUTINE RDNIST(FOO)
!DEC$ATTRIBUTES REFERENCE, ALIAS: "_RDNIST":: RDNIST
FOO = FOO * 2
return
END

C++:

extern "C" void __cdecl RDNIST(double *);
double bar = 3.14;
returnValues[0] = bar;
RDNIST(&bar);
returnValues[1] = bar;

Using the above code, the values of returnValues[0] and returnValues[1] are the same, both 3.14.

I have also tried using __stdcall instead of __cdecl, but the results are the same. Also, I tried compiling the Fortran lib with various calling conventions (C, REF... STDCALL, REF... etc). I have a feeling I'm missing some basic multi-language concept/rule. Any help will be greatly appreciated.

For reference, I am using MVS 2005 .NET, with IVF 9.1

Thanks!

Scott


0 Kudos
28 Replies
polakse
Beginner
1,237 Views
Alright, I removed all those specific libs (mentioned in the post below) from the "additional dependencies" list. I even set "Ignore All Default Libs" to "Yes," but when I build the Fortran and run "dumpbin" on the resulting lib, the list of Linker Directives is the same as before:

Linker Directives
-----------------
-defaultlib:ifconsol
-defaultlib:libifcoremt
-defaultlib:libifport
-defaultlib:libmmt
-defaultlib:LIBCMT
-defaultlib:libirc
-defaultlib:OLDNAMES
-defaultlib:ifconsol
-defaultlib:libifcoremt
-defaultlib:libifport
-defaultlib:libmmt
-defaultlib:LIBCMT
-defaultlib:libirc
-defaultlib:OLDNAMES



Also, I verified that there is no "main" program in any of the Fortran files. The Fortran lib (with the current settings) builds without any errors. But when I try to link into the C++ DLL, I get this error:
Error 1 error LNK2001: unresolved external symbol _MAIN__ libifcoremt.lib


0 Kudos
Steven_L_Intel1
Employee
1,237 Views
There is something else going on not evident from the data shown here. Please submit an issue to Intel Premier Support, attach a ZIP of your solution folder, and we'll take a look. Please ask that it be assigned to Steve Lionel.
0 Kudos
Steven_L_Intel1
Employee
1,237 Views
The problem turned out to be:

1) Different runtime library specified for C and Fortran project - these must be consistent
2) libifcoremt.lib explicitly listed as an additional dependency in C project - remove
3) msvcrt.lib listed as "ignore" in C project - remove
0 Kudos
polakse
Beginner
1,237 Views
Yes, indeed. I think item 1 was the big contributor to my problem. In my many attempts to debug this problem, I had tried building the c++ project with and without items 2 and 3, but it didn't seem to make a difference. However, the fact that my Fortran project was set as "Multithreaded" and the c++ project was set to "Multithreaded DLL," that was huge. Setting them both to "Multithreaded DLL" made everything happy.

Steve, you and this forum are an excellent resource and have helped me greatly. Thanks again.
0 Kudos
polakse
Beginner
1,237 Views
ahhh, just when I thought everything was working Sad smiley [:(]

So, I am able to compile and link the fortran lib into the c++ dll, and the dll works perfectly on my computer (it's a dll that provides a "user function" within the MathCAD software). however, when I distribute the dll to a co-worker's computer, using the exact same version of MathCAD, it does not work. This other person's computer does not have Intel Visual Fortran installed. Forgive my ignorance, but do the Fortran run-time libraries need to be present in order to use this c++ based dll that links a Fortran lib? If so, is there any way to compile/link the Fortran lib so it doesn't require IVF to run?

Hopefully this is the last hurdle. Thanks!
0 Kudos
Steven_L_Intel1
Employee
1,237 Views
The user does not need to have IVF installed, but they would need certain IVF run-time DLLs also present. They might also need certain MSVC DLLs present if they don't have the same version of Visual Studio.

You can change the Runtime Library settings in BOTH projects to be "Multithreaded" (not Multithreaded DLL) and it should do what you want.
0 Kudos
anthonyrichards
New Contributor III
1,237 Views

This sounds like a job for Dependency Walker, free software from www.dependencywalker.com . Run it past your .dll or .exe and it will show you what other
.dll's are required for it to run.

0 Kudos
andrey_
Beginner
1,237 Views
Thanks for the link and the idications.
0 Kudos
Reply