我正在尝试使用 fortran 调用 C 函数(项目需要这个)。因此,首先尝试通过 fortran 简单地调用一个非参数化的 void 函数。
请帮助我解决给定代码中的以下错误。
矩阵乘法的C代码:
#include <stdio.h>
extern "C"
{ void __stdcall mat();
}
void mat()
{
int m, n, p, q, c, d, k, sum = 0;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
int first[m][n];
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
int second[p][q];
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
int multiply[m][q];
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
}
int main()
{
mat();
return 0;
}
另外,我写的从fortran调用函数mat的代码是:
program mat_mult !This is a main program.
call mat()
stop
end
在执行 C 文件时,我收到以下错误:
matrix_mult.c:5:错误:预期标识符或字符串常量前的“(”
使用 F77 编译器执行 fortran 文件时,出现以下错误:
/tmp/ccQAveKc.o:在函数MAIN__':
matrix_mult.f:(.text+0x19): undefined reference to
mat_'collect2:ld 返回 1 退出状态
请帮助我识别错误/正确代码。谢谢你。