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

Linking Fortran into C

stevet96
Beginner
640 Views
I have a library that uses both C and Fortran code. I used to be able to build them together in the same project, however Visual Studio .Net 2003 does not allow this. I have created a separate project for both the C and Fortran codes. I have over 100 source files for both Fortran and C. Am I supposed to list every .obj file for all the fortran files if I use the C compiler to link? Is there a better way than to list every .obj file? I built the Fortran codeas a library and linked the library in with the C, but then I get redefinition warnings for every one of my Fortran functions. I have added a few of the Fortran .obj files to the C linker and I receive no redefinition errors. The "Mixed-Language" portion of the programming guide only says to include the .obj files, I'm just wondering if there is an easier way?
Thanks in advance,
Steve
0 Kudos
3 Replies
tongru_huo
Beginner
640 Views
Steve,
I am working on the same problem right now.
The way I do it is that I create a library for the fortran routines.
Then I link the C/C++ routines with the fortran library.
This way always works with CVF6.0 and VC++ 6.0 for me.
You can refer the following article to resolve the multiple redefinition of symbals.
Another thing you need to do is that you need to put dfor.lib (or dformt.lib for
multi-thread) at the beginging of other C libs. In VS.net, you do this in
project properties page / link /input.
I hope the above process work for you.
I followed the above process, I did have a clean compile and link in VS.net.
But the executable I got was not able to run. The running always exits with
code 255 (0xff). I don't know why. I posted my question in "How to make
CVF 6.0 and MS.net work together".
Regards
Tony
0 Kudos
stevet96
Beginner
640 Views
I don't have a problem with the fortran libraries that need to be added at linking time. What happens is I have a fortran function called DFWITR and Icall that function from C as DFWITR. When I link, the library has the definition for DFWITR in it, but Ialso have DFWITR prototyped for C which cause a redefinition warning. This only happens when I link the Fortran library in. However if I link in the .obj file that contains DFWITR, I don't get the function redefinition warning message.
On a side note,how can I link in dfor.lib before the default C libraries in Visual Studio .NET 2003? The default libraries are not listed. The only possibility I see is to not include all default libraries and list them individually. Am I missing something or did it become harder to order the libraries being linked in, in VS .Net 2003?
0 Kudos
tongru_huo
Beginner
640 Views
The prototype declaration in C should not cause a redefinition of function,
since a prototype is not a function defintion. C/C++ is quite clear about this
point.
The way you declare the fortran function prototype in C/C++ is as follows,
#if defined(cpp)
extern "C" {
endif
return_type dfwrit(argument_list);
#if defined(cpp)
}
endif
Be careful with the different convention of argument list in C/C++ and fortran and the return type, and function name.
As an example, if the fortran routine is as follows,
subroutine F77(d1,d2)
double precision d1, d2
C blah blah
return
end
Then the C prototype should be
void F77(double *d1, double *d2);
This should not cause redefinition of functions in C link for sure.
To put dfor.lib in the front of other c libs will guarantee that the fortran libs
is searched first to resolve symbols. You can do this in project setttings.
Tony
0 Kudos
Reply