2017-06-16 85 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