链接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,编译通过。所以是不是看报错加库就可以了呢,待以后验证吧

后来发现最后链接使用gfortran而不是gcc就不会出现少库的问题了

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值