ライブラリとしてサブディレクトリを持つFortranの分割プロジェクトがあります。
# ./CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (Simulation Fortran)
enable_language(Fortran)
add_subdirectory(lib)
add_executable(Simulation main.f90)
include_directories(lib)
add_dependencies(Simulation physicalConstants)
target_link_libraries(Simulation physicalConstants)
ルートディレクトリには、Fortranソースコードファイルが1つだけ含まれています。
! ./main.f90:
program simulation
use physicalConstants
implicit none
write(*,*) "Boltzmann constant:", k_b
end program simulation
そして私のサブディレクトリにlib
はCMakeLists.txt
、Fortranモジュールのソースファイルだけでなく別のファイルも含まれています。
# ./lib/CMakeLists.txt:
cmake_minimum_required (VERSION 2.8)
enable_language(Fortran)
project(physicalConstants)
add_library( physicalConstants SHARED physicalConstants.f90)
! ./lib/physicalConstants.f90:
module physicalConstants
implicit none
save
real, parameter :: k_B = 1.38e-23
end module physicalConstants
cmakeを使って作ってみました。Makephysicalconstants.mod
はlib
ディレクトリにを生成しますが、このファイルは次のビルドプロセス中に見つかりませんmain.f90.o
:
Fatal Error: Can't open module file 'physicalconstants.mod' for reading at (1): No such file or directory
ここで何が欠けていますか?
ターゲットAがターゲットBのモジュールを正常に使用するには、Bがモジュールファイルを格納するディレクトリがAのインクルードディレクトリの中にある必要があります。
これを実現する1つの方法はFortran_MODULE_DIRECTORY
、ターゲットBにプロパティを設定してから、そのプロパティのコンテンツを追加してAのディレクトリを含めることです。
あなたは古代のCMake2.8.0をサポートすると主張しています。そこでは、次のようなことをする必要があります。
add_executable(Simulation main.f90)
include_directories(lib)
# note that add_dependencies call is not necessary when you're actually linking
target_link_libraries(Simulation physicalConstants)
get_property(moduleDir TARGET physicalConstants PROPERTY Fortran_MODULE_DIRECTORY)
include_directories(${moduleDir})
より現代的なCMakeでは、代わりにこれを行うことができます:
add_executable(Simulation main.f90)
include_directories(lib)
target_link_libraries(Simulation physicalConstants)
target_include_directories(Simulation PUBLIC $<TARGET_PROPERTY:physicalConstants,Fortran_MODULE_DIRECTORY>)
そのための関数を作成することもできます。
function(LinkFortranLibraries Target)
target_link_libraries(Target ${ARGN})
foreach(Lib IN LISTS ARGN)
target_include_directories(Simulation PUBLIC $<TARGET_PROPERTY:${Lib},Fortran_MODULE_DIRECTORY>)
endforeach()
endfunction()
そして、次のように使用します。
add_executable(Simulation main.f90)
include_directories(lib)
LinkFortranLibraries(Simulation physicalConstants)
Fortran_MODULE_DIRECTORY
プロパティを使用しない場合、モジュールファイルはそれらを生成するターゲットのソースディレクトリに対応するバイナリディレクトリに保存されます。これは、ターゲットのプロパティから取得できます。これは、バリアント1とBINARY_DIR
まったく同じようFortran_MODULE_DIRECTORY
に使用できます。
ただし、CMake 2.8.0はターゲットプロパティをサポートしていないため、BINARY_DIR
その値を手動で「再構築」する必要があります。
add_executable(Simulation main.f90)
include_directories(lib ${CMAKE_CURRENT_BINARY_DIR}/lib)
target_link_libraries(Simulation physicalConstants)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加