【问题标题】:Linking library syntax with gfortran使用 gfortran 链接库语法
【发布时间】:2017-06-16 14:23:38
【问题描述】:

在 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 不是一个有效的命令。我也试过只用-R,还是一样的错误。
  • 还有-Wl,rpath....不记得细节了,我没用过。

标签: compilation fortran g++ gfortran dynamic-linking


【解决方案1】:

您不必在链接期间使用 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/LinuxTutorialMixingFortranAndC.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多