与gfortran和gcc静态链接的混合语言 [英] Mixed language statically linking with gfortran and gcc

查看:324
本文介绍了与gfortran和gcc静态链接的混合语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用C和Fortran编写的代码,我想编译成一个静态链接的可执行文件。如果我动态编译代码(对于gfortran使用 -fno-underscoring 选项),它一切正常。但是,我想将它链接到一个.so文件中,静态链接大多数所需的库,然后动态链接到 libkrb5 ,非常类似于本博文

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.

我按照上一篇博文中的步骤操作,并且设法编译.so库没有任何问题。 nm 表明它状态良好,Fortran子程序和C函数显示出来:

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

第一个函数是用Fortran编写的,第二个函数是用C编写的,它们没有下划线或任何东西,所以它们应该链接。我的主程序(在Fortran中)与其他Fortran源文件一样编译。但是,当我尝试链接时,出现错误:

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返回1退出状态

collect2: ld returned 1 exit status

main_program .o ,我可以看到有

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

00000000 T MAIN__

一切都按顺序排列,为什么它没有连接?

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

推荐答案

好的,看起来它比我想象中的更简单!您只需要使用gfortran进行链接,并在最后一步忽略链接gfortran:

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

似乎现在就工作!

这篇关于与gfortran和gcc静态链接的混合语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆