Mixed language statically linking with gfortran and gcc

倾然丶 夕夏残阳落幕 提交于 2019-12-05 06:45:41

问题


I have some code written in C and Fortran that I want to compile into a statically-linked executable. If I compile the code dynamically (using the -fno-underscoring option for gfortran), it all works fine. However, I want to link it into a .so file, statically linking most of the needed libraries, and then link dynamically to libkrb5, very much like the method described in this blog post.

I followed the steps in the previous blog post, and I managed to compile the .so library without any problems. nm shows that it is in good shape, with my Fortran subroutines and C functions showing up:

[...]001020b9 T turnover
[...]000d31ea T initio

The first function is written in Fortran, and the second in C. They don't have underscores or anything, so they should link. My main program (in Fortran) is compiled like the other Fortran source files. When I try to link, though, I get an error:

gfortran  -m32  main_program.o -o program_static  -L./ -llibname -lkrb5 -lgssapi_krb5 -lsasl2 -lgfortran
/usr/lib/gcc/i486-linux-gnu/4.3.3/../../../../lib/crt1.o: In function `_start':
/build/buildd/glibc-2.9/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'

collect2: ld returned 1 exit status

In main_program.o, I can see that there's

00000000 T MAIN__

Everything looks in order, so why is it not linking?


回答1:


OK, it appears that it was faaar simpler than what I envisaged! You just need to link using gfortran, and ignore linking gfortran in that final step:

gfortran -static-libgfortran -m32  main_program.o -o program_static\
         -L./ -llibname -lkrb5 -lgssapi_krb5 -lsasl2

Seems to work now!



来源:https://stackoverflow.com/questions/1302818/mixed-language-statically-linking-with-gfortran-and-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!