2017-06-16 65 views
1

在C++库中的语法,如果我想要做一个自定义编译(意为连接额外的库),我通常做到以下几点:链接与gfortran

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是一个有效的命令。

请让我知道,谢谢。

+0

为什么它似乎?怎么了? –

+0

编译器说rpath不是一个有效的命令。我也尝试过使用-R,但仍然是相同的错误。 – mle0312

+0

以及-Wl,rpath ......不记得细节,我不使用它。 –

回答

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 

这将允许调用函数从LIB

./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