在Visual Studio 2008中逐步调用intel Fortran 11的C函数 [英] Calling C function from intel Fortran 11 in Visual studio 2008 step by step procedure

查看:163
本文介绍了在Visual Studio 2008中逐步调用intel Fortran 11的C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我一步一步回答示例,如何在Visual Studio 2008中从Fortran调用C函数。我的Fortran编译器正在Visual Studio 2008中工作。在哪里我应该保留C和Fortran文件,为此我需要一个C编译器或不?

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?.

推荐答案

您最好使用MinGW之类的Windows构建环境。安装它并确保包含gcc.exe,make.exe等的MinGW目录在你的路径中。你需要帮助吗?

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?

接下来你编写C文件并将其编译到一个目标文件中:

Next you write the C file and compile it to an object file:

gcc -c -o c_file.obj c_file.c

例如,你的c文件可能是:

For example, your c file might be:

#include <stdio.h>

void my_c_function() {
    printf("Entering my_c_function()...\n");
}

现在您需要为Fortran编译器提供C函数的接口:

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

您将需要告诉链接器关于目标文件c_file.obj。我假设你正在使用Visual Studio IDE。转到项目属性 - >链接器 - >输入,并将c_file.obj添加到其他依赖项。它应该工作得很好。

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.

这篇关于在Visual Studio 2008中逐步调用intel Fortran 11的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆