我有关于混合语言编程(C/C++ 和 FORTRAN)的问题
使用 gcc 和 gfortran。我搜索了很多“将 fortran 与
语言 X”并且无法解决这个问题。
我不确定这是链接问题还是编译器问题,或者两者兼而有之。
我创建了三个文件,并使用 GNU Autotools 来构建
粗糙的应用程序,但应该能够从命令构建应用程序
独立线路。
C 文件 (main.c) 将是驱动应用程序,它调用多个
FORTRAN 函数:
/* this is a simple program */
#include <stdio.h>
/* add the extern function definition */
#include "fooonly.h"
// this is not working for the mixed language programming stuff yet...
/* this is the main program section */
int main( int argc, char *argv[] )
{
int a = 36;
int b = 24;
int c = 0;
printf( "hi mom\n" );
/* now call a FORTRAN function from C */
c = NGCD( &a, &b );
printf( "NGCD(%d,%d)=%d\n", a, b, c );
return 0;
}
Fortran 函数最常包含 FORTRAN 77(但是
也可以包含 FORTRAN90/95 代码),看起来像:
c$$$ The following introductory example in FORTRAN 77 finds the
c$$$ $ greatest common divisor for two numbers A and B using a
c$$$ $ verbatim implementation of Euclid's algorithm.
FUNCTION NGCD(NA, NB)
IA = NA
IB = NB
1 IF (IB.NE.0) THEN
ITEMP = IA
IA = IB
IB = MOD(ITEMP, IB)
GOTO 1
END IF
NGCD = IA
RETURN
END
使用开发。 Studio 6/Compaq Digital Fortran 6.0,效果很好。在
事实上,我不必使用 #define ifdef __cplusplus/#endif 并且可以
只需创建一个如下所示的 C 文件:
/* this is a simple program */
#include <stdio.h>
/* add the extern function definition */
extern "C" int __stdcall NGCD( int *a, int *b );
/* this is the main program section */
int main( int argc, char *argv[] )
{
int a = 36;
int b = 24;
int c = 0;
printf( "hi mom\n" );
/* now call a FORTRAN function from C */
c = NGCD( &a, &b );
printf( "NGCD(%d,%d)=%d\n", a, b, c );
return 0;
}
并使用上面的 FORTRAN 列表、应用程序链接对其进行编译,
运行并生成正确的结果。
C:\fooonly>fooonly.exe
hi mom
NGCD(36,24)=12
C:\fooonly>
当我尝试在 MinGW 上使用 GNU Autotools 重复此过程或
OSX,我继续收到以下错误:
macbook:makecheck $ make
gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"fooonly\" -DVERSION=\"1.0.2\" -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gfortran -g -O2 -o fooonly main.o ngcd.o
Undefined symbols for architecture x86_64:
"_NGCD", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [fooonly] Error 1
macbook:makecheck $
其中 Makefile(由 GNU Autotools 生成)基本上包含
以下命令:
macbook:makecheck $ gcc -c main.c
macbook:makecheck $ gfortran -c ngcd.f
macbook:makecheck $ gcc -o fooonly main.c ngcd.o
Undefined symbols for architecture x86_64:
"_NGCD", referenced from:
_main in cc9uPBWl.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
macbook:makecheck $
我的configure.in脚本仅包含:
AC_INIT(main.c)
AM_INIT_AUTOMAKE(fooonly, 1.0.2)
## C/C++ compiler section
AC_PROG_CC
## fortran section
AC_PROG_F77
## output section
AC_OUTPUT(Makefile)
这本质上是
macbook:makecheck $ gcc -c main.c
macbook:makecheck $ gfortran -c ngcd.f
macbook:makecheck $ gcc -o fooonly main.c ngcd.o
right?
我正在尝试在多个平台(Linux、Win32/64、OSX、
等)并希望使用 GNU Autotools,我知道这是用
其他开源项目,但这些项目的 configure.in 脚本
项目远远超出了我的 GNU Autotools 新手能力范围,我得到了
试图解码它们有点不知所措。
我猜这与以下内容有关:
1)我在configure.in脚本中使用的定义,
2)我不包括一些神奇的开关
(即 -fno-第二个下划线?),或者
3)两者的某种组合?
我已经接近了吗?如果是,我该如何构建应用程序?