使用 gfortran 链接库语法
Linking library syntax with gfortran
在 C++ 中,如果我想进行自定义编译(意味着 link 个额外的库),我通常会执行以下操作:
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 有效,表明我设法 link 并包含库。但是,gfortran 似乎无法将 rpath 识别为有效命令。
请告诉我,谢谢。
您不必在 linking 期间使用 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.
有用link:
http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
在 C++ 中,如果我想进行自定义编译(意味着 link 个额外的库),我通常会执行以下操作:
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 有效,表明我设法 link 并包含库。但是,gfortran 似乎无法将 rpath 识别为有效命令。
请告诉我,谢谢。
您不必在 linking 期间使用 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.
有用link:
http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html