我想在Linux中创建一个Fortran共享库,该库静态链接到gcc
库。
例如,我有hello.f
:
function hello()
print *, "Hello World!"
end function hello
当我使用共享的gcc
链接时,一切正常
$ gcc hello.f -shared -fPIC -o hello.so -lgfortran
$ ldd hello.so
linux-vdso.so.1 => (0x00007ffe3df5d000)
libgfortran.so.4 => /lib64/libgfortran.so.4 (0x00007fb1e59eb000)
libquadmath.so.0 => /lib64/libquadmath.so.0 (0x00007fb1e57af000)
libm.so.6 => /lib64/libm.so.6 (0x00007fb1e54ad000)
libc.so.6 => /lib64/libc.so.6 (0x00007fb1e50e0000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fb1e4eca000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb1e5fc9000)
当我尝试静态链接时,出现错误:
$ gcc hello.f -shared -fPIC -o hello.so -static-libgcc -Wl,-Bstatic -lgfortran -Wl,-Bdynamic
ld: /usr/lib/gcc/x86_64-redhat-linux/7/libgfortran.a(transfer.o): relocation R_X86_64_32S against `.rodata.bswap_array' can not be used when making a shared object; recompile with -fPIC
...
ld: final link failed: Bad value
在c ++中,相同的方法效果很好:
#include <iostream>
int hello(){
std::cout << "Hello, World!\n";
return 0;
}
链接到静态gcc
库
$ gcc hello.cpp -shared -fPIC -o hello.so -static-libgcc -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic
$ ldd hello.so
linux-vdso.so.1 => (0x00007ffd89192000)
libc.so.6 => /lib64/libc.so.6 (0x00007fdb8dfb2000)
/lib64/ld-linux-x86-64.so.2 (0x00007fdb8e6c7000)
有人知道为什么libgfortran.a
不能用-fPIC
编译libstdc++.a
吗?
有什么解决方法吗?