鏈接fortran和c的undefined reference to問題

  • 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,編譯通過。所以是不是看報錯加庫就可以了呢,待以後驗證吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章