gpt4 book ai didi

c++ - 混合编程 : Calling FORTRAN from C++

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:50 25 4
gpt4 key购买 nike

我正在尝试使用命令行编译一个混合程序。

编译工具是intel visual fortran 2013和VS 2013。

我搜索了一下,发现使用 dlllib 更容易。

所以我尝试将我的 fortran 源代码编译为 dll 文件

我的 Fortran 来源:

!  forsubs.f90 
!
! FUNCTIONS/SUBROUTINES exported from FORSUBS.dll:
! FORSUBS - subroutine
!
INTEGER*4 FUNCTION Fact (n)
!DEC$ ATTRIBUTES DLLEXPORT::Fact
INTEGER*4 n [VALUE]
INTEGER*4 i, amt
amt = 1
DO i = 1, n
amt = amt * i
END DO
Fact = amt
write(*,*)"Mixed calls succeed!"
END
SUBROUTINE Pythagoras (a, b, c)
!DEC$ ATTRIBUTES DLLEXPORT::Pythagoras
REAL*4 a [VALUE]
REAL*4 b [VALUE]
REAL*4 c [REFERENCE]
c = SQRT (a * a + b * b)
END

我的 C++ 来源:c_main.cpp

/*     File CMAIN.C   */

#include <stdio.h>

extern int __stdcall fact(int* n);
extern void __stdcall pythagoras(float* a, float* b, float *c);

int main()
{
float c;
printf("Factorial of 7 is: %d\n", FACT(7));
PYTHAGORAS (30, 40, &c);
printf("Hypotenuse if sides 30, 40 is: %f\n", c);
}

首先我编译了我的 fortran 源代码:

ifort /dll forsubs.f90

并得到一个forsubs.dll 和一个forsubs.lib

其次,我编译了我的 C++ 源代码:

cl c_main.cpp /link forsubs.lib

但是,我得到了一个错误:

D:\c_f\c_f_dll\c_main.cpp(11) : error C3861: “FACT”:  identifier not found
D:\c_f\c_f_dll\c_main.cpp(12) : error C3861: “PYTHAGORAS”: identifier not found

此外,我尝试以另一种方式加载 dll,使用:

 HINSTANCE hLibrary=LoadLibrary("forsubs.dll"); 
if(hLibrary==NULL)
{
cout<<"can't find the dll file"<<endl;
return -1;
}

但我每次都收到“找不到 dll 文件”的提示,而我确定 forsubs.dll 位于文件夹中。

我不擅长C++,不知道怎么编译,求助!

编辑

奇怪!我按照@Mikael Persson 的回答编辑了我的程序,但仍然出现错误。

error LNK2019: unresolved external symbol _FACT@4 referenced in function _main
error LNK2019: unresolved external symbol _PYTHAGORAS@12 referenced in function _main

另外,我用谷歌搜索了一下,有人说 __stdcall 不是必需的,我删除了但仍然有错误:

error LNK2019: unresolved external symbol _FACT referenced in function _main
error LNK2019: unresolved external symbol _PYTHAGORAS referenced in function _main

我认为这可能是与this 相同的问题.但是自己解决不了。

附言:我的 dlllib 的信息:

D:\c_f\c_f_dll>dumpbin /exports forsubs.lib
Microsoft (R) COFF/PE Dumper Version 12.00.30501.0
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file forsubs.lib

File Type: LIBRARY

Exports

ordinal name

FACT
PYTHAGORAS

Summary

C3 .debug$S
14 .idata$2
14 .idata$3
8 .idata$4
8 .idata$5
C .idata$6

D:\c_f\c_f_dll>dumpbin /exports forsubs.dll
Microsoft (R) COFF/PE Dumper Version 12.00.30501.0
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file forsubs.dll

File Type: DLL

Section contains the following exports for forsubs.dll

00000000 characteristics
5477F1C5 time date stamp Fri Nov 28 11:53:41 2014
0.00 version
1 ordinal base
2 number of functions
2 number of names

ordinal hint RVA name

1 0 00001000 FACT
2 1 000010B0 PYTHAGORAS

Summary

1000 .data
1000 .pdata
1000 .rdata
1000 .reloc
1000 .text

编辑2

Also, I have tried to add a header:
#ifdef FORSUBS_EXPORTS
#define FORSUBS_API __declspec(dllexport)
#else
#define FORSUBS_API __declspec(dllimport)
#endif

还是不行。

最佳答案

C/C++ 区分大小写,因此如果调用 FACT,则它必须声明为 FACT,而不是 fact。或者反过来。

并且您需要将您的函数(在 C++ 代码中)声明为 extern "C",以禁用 C++ 特定的名称重整,否则会假定。

__stdcall 确实是 Fortran 函数的正确调用约定(默认情况下)。

Fortran 不区分大小写(默认情况下,但可以通过编译器选项更改,这很愚蠢)。我相信英特尔 Fortran 编译器会为 Fortran 函数(例如,名为“Fact”的函数将导出为“FACT”)生成全大写符号(用于链接目的)。因此,您需要在 C++ 声明中匹配这种情况。我不确定 Fortran 符号的处理方式(全大写还是全小写),所以,如果一个不起作用,请尝试另一个。

编辑 2:

至于原型(prototype),我相信这些是 C++ 中的正确形式(我不确定,因为我对 Fortran 很生疏):

#include <stdint.h>  // in C, or <cstdint> in C++ (but must use std::int32_t)

extern "C" __declspec(dllimport) int32_t __stdcall FACT(int32_t n);
extern "C" __declspec(dllimport) void __stdcall PYTHAGORAS(float a, float b, float *c);

注意对于一个4字节的整数,你需要使用int32_t,对于Fortran中的[VALUE]类型,它也应该按值传递C/C++...至少,这是我对它的理解(但就像我说的,我不是 Fortran 专家)。

关于c++ - 混合编程 : Calling FORTRAN from C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27182209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com