C++如何称呼Fortran 77的公共块
How C++ call Fortran 77's common blocks
我刚开始编程,我想在C++代码中调用Fortran 77公共块。事实上,我读过一些问答;类似于我的,但我不太清楚。。。。
这个公共块是由另一个Fortran 77子程序定义的。
示例代码为:
common.inc:
!test common block:
real delta(5,5)
common /test/ delta
!save /test/ delta ! any differences if I comment this line?
tstfunc.f
subroutine tstfunc()
implicit none
include 'common.inc'
integer i,j
do i = 1, 5
do j = 1, 5
delta(i,j)=2
if(i.ne.j) delta(i,j)=0
write (*,*) delta(i,j)
end do
end do
end
tst01.cpp
#include <iostream>
extern "C"
{
void tstfunc_();
};
void printmtrx(float (&a)[5][5]){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
std::cout<<a[j][i]<<'t';
a[j][i]+=2;
}
std::cout<<std::endl;
}
}
int main()
{
//start...
tstfunc_();
printmtrx(delta);//here i want to call delta and manipulate it.
return 0;
}
如果我想将delta
(来自common.inc)传递给C++函数printmtrx()
,该怎么办?
除了行/列的主要顺序问题(5x5矩阵在C代码中会出现转置)之外,也许您可以按以下方式进行(请参阅本教程中关于公共块的部分):
tstfunc1.f
subroutine tstfunc()
implicit none
real delta(5, 5)
common /test/ delta
integer i,j
do i = 1, 5
do j = 1, 5
delta(i,j)=2
if(i.ne.j) delta(i,j)=0
write (*,*) delta(i,j)
end do
end do
end
tst01.cc
#include <iostream>
extern "C" {
void tstfunc_();
extern struct{
float data[5][5];
} test_;
}
void printmtrx(float (&a)[5][5]){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
std::cout << a[i][j] << 't';
a[i][j] += 2;
}
std::cout << std::endl;
}
}
int main()
{
//start...
tstfunc_();
printmtrx(test_.data);//here i want to call delta and manipulate it.
return 0;
}
然后为了编译:
gfortran -c -o tstfunc1.o tstfunc1.f
g++ -o tst tst01.cc tstfunc1.o -lgfortran
请注意,C中的2D数组以行为主,而在FORTRAN中,它们以列为主,因此您需要用一种或另一种语言切换数组索引。
相关文章:
- 使用 VS2015 在 Windows 10 上构建 Fortran .lib x64 位并将其链接到 C++
- 将 OR 逻辑运算符从 C++ 转换为 Fortran
- Fortran 和 C++ 的混合编程:Fortran 不能调用C++子程序
- 如何在C++中获取真实值精度 与Fortran相同(Pararel Studio XE编译器)
- 读取 C++ 中的 Fortran 数据类型
- 重新编译我们的FORTRAN代码并在C++中使用后,我们的system()或C_str()命令无法正常工作
- 使用C++和fortran构建R包,Mac
- 如何将C++17 stdc++fs库链接到混合语言(C++和Fortran)CMake项目
- 混合编程 - 包括C++标头到 Fortran
- 编译 Fortran 和C++程序并从英特尔编译器链接
- 与 MSVC 一起使用的 MinGW 的 Fortran 模块导致未解析的符号"__exmod_MOD_value"
- 将指向 1D Fortran 数组的指针传递给使用 C_LOC() 进行C++会导致垃圾值
- 将分配给C++数组传递给 Fortran 子例程
- 在 Fortran 中泛化特定声明类型的操作
- CPP -D 选项,用于预处理 Fortran 代码
- 通过矢量下标访问 Fortran 数组,相当于 cpp
- 如何将矢量从C 传递到Fortran
- 将字符串从 Fortran 传递到 c++
- 是否可以从C 调用FORTRAN接口
- C /FORTRAN项目由Visual Studio 2010/Intel 2013创建,将不会在Visual St