在 Visual Studio 2008 中从 Intel Fortran 11 调用 C 函数分步过程

发布于 2024-12-01 15:10:19 字数 141 浏览 2 评论 0原文

请给我一个逐步的答案,例如如何在 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?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-12-08 15:10:19

您最好使用 Windows 构建环境,例如 MinGW。安装它并确保包含 gcc.exe、make.exe 等的 MinGW 目录位于您的路径中。您需要帮助进行设置吗?

接下来,编写 C 文件并将其编译为目标文件:

gcc -c -o c_file.obj c_file.c

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

#include <stdio.h>

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文