Please give me a step by step answer with example, how to call a C function from Fortran in visual studio 2008. My Fortran compiler is working in visual studio 2008. Where I should keep the C and Fortran files and for this I need a开发者_开发知识库 C compiler or not?.
You are best off using a Windows build environment such as MinGW. Install that and make sure the MinGW directory containing gcc.exe, make.exe etc is in your path. Do you need help setting that up?
Next you write the C file and compile it to an object file:
gcc -c -o c_file.obj c_file.c
For example, your c file might be:
#include <stdio.h>
void my_c_function() {
printf("Entering my_c_function()...\n");
}
Now you need to provide an interface to the Fortran compiler for the C function:
module foo
use ISO_C_Binding
interface
subroutine my_c_function() bind(C,name="my_c_function")
end subroutine
end interface
end module foo
program main
use foo
call my_c_function()
end program
You will need to tell the linker about the object file c_file.obj. I'm assuming you're using the Visual Studio IDE. Go to Project Properties -> Linker -> Input, and add c_file.obj to "Additional dependencies". It should then work fine.
精彩评论