- c文件
#include<stdio.h>
void show_(float *);
int main(){
float a=100;
show_(&a);
}
- f90文件
subroutine show(a)
real :: a
print*, a
return
end subroutine show
- Makefile
test : test.c show.f90
gfortran -c show.f90 -o show.o
gcc -c test.c -o test.o
gcc -o test.exe test.o show.o
结果报错
show.o:show.f90:(.text+0x3f): undefined reference to `_gfortran_st_write'
show.o:show.f90:(.text+0x58): undefined reference to `_gfortran_transfer_real_write'
show.o:show.f90:(.text+0x64): undefined reference to `_gfortran_st_write_done'
网上查到是少了静态链接库,查到的那个博客加了一大堆库,大概用不到那么多吧。看报错,少的大概是gfortran
库?不管了试试看
- Makefile (
-lgfortran
似乎只能放在.o
文件后面,否则还是报前面一样的错误)
test : test.c show.f90
gfortran -c show.f90 -o show.o
gcc -c test.c -o test.o
gcc -o test.exe test.o show.o -lgfortran
又报新的错误
C:/MyProgram/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(write.o):(.text$get_precision+0x160): undefined reference to `quadmath_snprintf'
C:/MyProgram/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(write.o):(.text$get_float_string+0x16b): undefined reference to `quadmath_snprintf'
C:/MyProgram/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(write.o):(.text$get_float_string+0xa2f): undefined reference to `quadmath_snprintf'
C:/MyProgram/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(write.o):(.text$get_float_string+0x162c): undefined reference to `quadmath_snprintf'
C:/MyProgram/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(write.o):(.text$get_float_string+0x170a): undefined reference to `quadmath_snprintf'
我似乎发现了某种规律
- Makefile
test : test.c show.f90
gfortran -c show.f90 -o show.o
gcc -c test.c -o test.o
gcc -o test.exe test.o show.o -lgfortran -lquadmath
ok,编译通过。所以是不是看报错加库就可以了呢,待以后验证吧
后来发现最后链接使用gfortran
而不是gcc
就不会出现少库的问题了