在 C++ 中,如果我想进行自定义编译(即链接其他库),我通常会执行以下操作:
g++ filename -o outputname -I/include_libraries_here -L/link_libraries_here -rpath=path_for_dynamic_linking_here
我将如何使用 gfortran 来做类似的事情。我试过:
gfortran filename -o outputname -I/include_libraries_here -L/link_libraries_here -rpath=path_for_dynamic_linking_here
到目前为止,语法 -I 和 -L 有效,这表明我成功链接并包含了这些库。但是,gfortran 似乎不将 rpath 识别为有效命令。
请告诉我并谢谢您。
您不必在链接期间使用 rpath。当然可以。
看看这里:
#include <stdio.h>
void fun() {
printf("Hello from C\n");
}
我们可以像这样创建共享库:
gcc -fPIC -shared -o libfun.so fun.c
然后,我们可以编译以下代码:
program hello
print *, "Hello World!"
call fun()
end program hello
像这样:
# without -rpath
gfortran -fno-underscoring -o hello -L. -lfun hello.f90
# in this case you have to make sure libfun.so is in LD_LIBRARY_PATH
# with rpath
gfortran -fno-underscoring -o hello -L. -Wl,-rpath=`pwd` -lfun hello.f90
# in this case, library will be properly located at runtime
这将允许从共享库调用函数
./hello
Hello World!
Hello from C
-rpath是ld的参数
-rpath=dir
Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated
and passed to the runtime linker, which uses them to locate shared objects at runtime.
有用的链接:
http://www.yolinux.com/TUTORIALS/LinuxTutorialMishingFortranAndC.html http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)