完整的fortran90调用c++函数的代码
时间: 2023-08-22 20:05:14 浏览: 60
以下是一个完整的Fortran90调用C++函数的示例代码:
C++代码:
```cpp
// example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
extern "C" {
int add_numbers(int a, int b);
}
#endif
```
```cpp
// example.cpp
#include "example.h"
int add_numbers(int a, int b) {
return a + b;
}
```
Fortran90代码:
```fortran
! main.f90
program main
implicit none
interface
integer function add_numbers(a, b) bind(C, name='add_numbers')
import :: C_INT
integer(C_INT), value :: a, b
end function add_numbers
end interface
integer :: a, b, sum
a = 1
b = 2
sum = add_numbers(a, b)
print *, 'The sum of', a, 'and', b, 'is', sum
end program main
```
在Fortran代码中,使用INTERFACE声明了一个add_numbers函数的接口,并且通过BIND(C)指定了C++函数的命名方式。IMPORT语句指定了使用的数据类型为C_INT。在程序中,可以直接调用add_numbers函数并将其返回值赋给sum变量,最后输出结果。注意,在编译时需要将C++代码和Fortran90代码同时编译,并在链接时链接C++库。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)