CMake找不到Fortran模块文件

克里斯

我在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源代码文件:

! ./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.modlib目录中生成,但是在构建过程中找不到该文件main.f90.o

Fatal Error: Can't open module file 'physicalconstants.mod' for reading at (1): No such file or directory

我在这里想念什么?

Angew不再为SO感到骄傲

为了使目标A成功使用目标B中的模块,B存储模块文件的目录必须在A的包含目录中。

变体1

一种实现方法是Fortran_MODULE_DIRECTORY在目标B上设置属性,然后将该属性的内容添加到A的目录中。

您声称支持古老的CMake 2.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)

变体2

如果不使用该Fortran_MODULE_DIRECTORY属性,则模块文件将存储在与生成它们的目标的源目录相对应的二进制目录中。可以从目标的property中检索该属性BINARY_DIR,您可以像Fortran_MODULE_DIRECTORY在变体1中一样使用它

但是,CMake 2.8.0不支持target属性BINARY_DIR,因此您必须手动“重建”其值:

add_executable(Simulation main.f90)
include_directories(lib ${CMAKE_CURRENT_BINARY_DIR}/lib)
target_link_libraries(Simulation physicalConstants)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章