Call C/C++ code form a fortran program in visual studio? (How to compile mixed C and fortran code in visual studio)

StackOverflow https://stackoverflow.com/questions/3135296

Question

i am looking for a way, how i can integrate a c++ code with fortran code (i want simply call some C/C++ functions in the fortran code).

I have found some proposals for gcc or console compilers, but i have not any idea how to translate this approach to solve integrationproblem within the visual studio.

At the time I am thinking about creating a dll form c++ code and calling it from Fortran code.

Has someone already seen a solution? Or what is about overhead for calling function from dll? My fortran code transfers a lot of memory into C function, is there any problems, if i would solve this problem with dll?

thx.

PS I am using Visual Studio 2008 Prof and Intel compilers 10

PPS I think, i have to specify more concrete, what i want: i want to compile a fortran project in visual studio, which uses some C functions.

Was it helpful?

Solution

There is a new way to do this that has many advantages -- use the Fortran 2003 ISO C Binding. This is a standard and therefore largely OS and language independent way of interfacing Fortran and C (and any language that will use C calling conventions). Intel Fortran 11 supports along with numerous other compilers -- not sure about version 10. Using the ISO C Binding, you can match any C name (any case), don't have to worry about underscores (and variations between compilers) and can specify the types and calling methods (by reference, by value) for the arguments. Intel provides some examples in a folder with their compiler; there are also examples in the gfortran manual and a discussion of additional considerations for Windows. There are previous questions & answers here and on the Intel Fortran forum.

OTHER TIPS

I integrated C and Fortran about 20 years ago and maintained this integration up to 5 years ago. The tricks I used were:

  • I noticed that the Fortran compiler puts all symbols in uppercase, so make sure your C/C++ functions are written in uppercase as well. To verify how symbols are put in the .OBJ file, use DUMPBIN.
  • The Fortran compiler does not understand the name-mangling done by the C++ compiler. Compile all your C++ functions using the C style convention (using extern "C")
  • Arguments in Fortran are always put on the stack using references/pointers. Therefore, use pointer-arguments in your C function.

To be honest, I gave up integrating C and Fortran when I switched to VS2005, so things might have changed since then. Nevertheless, it's still a good idea to use DUMPBIN to see what kind of symbols the Fortran compiler produces, and adjust the compilation of C/C++ sources to fit with that.

We do it where I work.

Assuming you are using the Intel Fortran compiler, look up its docs. By default Intel Fortran passes everything by reference and (I believe) uses the C calling convention, with an all caps identifier. Strings are a particular issue, as Fortran likes to pass the length as a hidden parameter, with a compiler setting for where it goes in the parameter list.

A wise programer doesn't rely on defaults (where a mistake can lead to undefined behavior), and will use the intel INTERFACE statements to specify calling convention, parameter passing, and the link name for the routine. The information on this page (ATTRIBUTES Properties and Calling Conventions) is a must-read. In particular you need it to understand the arcane rules for when and where string length parameters will be passed. I have a printout of it that I keep on my person. :-)

One other thing to note is that versions of VisualStudio past 6 don't like mixed Fortran and C projects. We solved the problem by creating custom project files calling out to makefile, but that's a PITA. I'd suggest going with the flow and using separate projects unless you are doing this a lot like we are.

Solution found: solution link

i have had several problem with linking, which could be solved with adding in project properties.

code for testing:

#include <stdio.h>

extern "C"
{
    void f() 
    {
        printf("hi from c\n mega test");
    }
}

fortran code

PROGRAM HelloWorld
use, intrinsic :: iso_c_binding
implicit none 
interface
    subroutine f( ) bind( c )
        use, intrinsic :: iso_c_binding
    end subroutine f   
end interface  
call f
END PROGRAM HelloWorld

on demand i can upload the testproject. thanks all, hopefully it was my last problem with c and fortran

I was able to build obj from fortran sources thanks to the Custom Build Tools of Visual Express 2010. I guess it is also possible in Visual Studio.

If you want to mix C and Fortran together, there is a good tutorial here. It was written for gcc compilers but you should be able to learn how to deal with name mangling easily.

Depending on the compiler, compiled subroutines/functions are Uppercase/lowercase, with a trailing underscore, with a leading underscore,... For a succesfull linkage, you could use dumpbin tools to see how the name appears in the objectfile.

An other way is to use iso_c_binding modules, but it is available with Fortran 2003 only.

This is the how it works with gcc and console

c.c:

#include <stdio.h>

void f_()
{
    printf("Hi from C\n");
} 

fortran.f90

PROGRAM HelloWorld
   CALL f
END PROGRAM HelloWorld

Makefile

SRCDIR=.

all: clean release run

release:
    gcc -c c.c -o c.out 
    gfortran -c fortran.f90 -o fortran.out
    gfortran -o out.exe fortran.out c.out
run:
    out.exe

clean:
    @$(ZSHMAGIC)  rm -rf *.exe core* *.o a.out 2> /dev/null

One other question: have i always add '_' after c-function name, which i use in the fortran program?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top