【问题标题】:Cmake not changing build flagsCmake不改变构建标志
【发布时间】:2020-04-27 17:31:52
【问题描述】:

考虑以下简单的 openMP 程序:

program hello
    use OMP_LIB
    implicit none
    integer   :: nthreads, tid

    !$OMP PARALLEL PRIVATE(nthreads, tid)
    tid = omp_get_thread_num()
    nthreads = omp_get_num_threads()
    ! print *, "hello"
    print *,"Thread:", tid, "of", nthreads
    !$OMP END PARALLEL
end program hello

当我编译它时

gfortran -fopenmp hello.90 -o hello.x

我得到的输出为:

主题:2 / 4

主题:1 of 4

主题:4 个中的 0 个

主题:3 / 4

正如预期的那样。

现在如果我考虑以下CMakeLists.txt 文件

cmake_minimum_required(VERSION 2.8.9)
project (hello Fortran)
enable_language(Fortran)

IF(NOT CMAKE_BUILD_TYPE)
  message("NO BUILD TYPE PROVIDED")
  message("DEFAULT BUILD TYPE SET TO 'Release'")
  set(CMAKE_BUILD_TYPE 'Release')
ENDIF()
IF(${CMAKE_BUILD_TYPE} MATCHES RELEASE OR ${CMAKE_BUILD_TYPE} MATCHES Release)
  message("MAKING RELEASE BUILD")
  set(CMAKE_Fortran_FLAGS_RELEASE "-fopenmp")
  message("FLAGS: ${CMAKE_Fortran_FLAGS_RELEASE}")
ENDIF()

add_executable(hello.x hello.f90)

考虑到上面的文件是正确的,它应该添加 omp 标志来制作文件,因此编译和运行相同,但是

$ mkdir build; cd build
$ cmake ..
-- The Fortran compiler identification is GNU 6.1.0
-- Checking whether Fortran compiler has -isysroot
-- Checking whether Fortran compiler has -isysroot - yes
-- Checking whether Fortran compiler supports OSX deployment target flag
-- Checking whether Fortran compiler supports OSX deployment target flag - yes
-- Check for working Fortran compiler: /usr/local/bin/gfortran
-- Check for working Fortran compiler: /usr/local/bin/gfortran - works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /usr/local/bin/gfortran supports Fortran 90
-- Checking whether /usr/local/bin/gfortran supports Fortran 90 - yes
NO BUILD TYPE PROVIDED
DEFAULT BUILD TYPE SET TO 'Release'
MAKING RELEASE BUILD
FLAGS: -fopenmp
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user/Programs/omp/build

$
$ make
Scanning dependencies of target hello.x
[ 50%] Building Fortran object CMakeFiles/hello.x.dir/hello.f90.o
[100%] Linking Fortran executable hello.x
Undefined symbols for architecture x86_64:
  "_omp_get_num_threads_", referenced from:
      _MAIN__ in hello.f90.o
  "_omp_get_thread_num_", referenced from:
      _MAIN__ in hello.f90.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [hello.x] Error 1
make[1]: *** [CMakeFiles/hello.x.dir/all] Error 2

这与缺少-fopenmp 标志相同。如何添加?

【问题讨论】:

  • 请不要链接到随时可能无法访问的外部文件。在此处复制并粘贴内容。它甚至没有那么长。只需复制和粘贴即可。
  • 请注意,cmake -LA 仅显示 CACHE 的变量。您设置了“正常”变量CMAKE_Fortran_FLAGS_RELEASE,因此它不会存储在缓存中。但是,这样的变量设置应该可以工作......只要您正确设置它。你展示了这么少的代码,所以很难说什么是错的。请创建minimal reproducible example 并将其添加到问题帖子(不是链接)。

标签: cmake fortran


【解决方案1】:

CMAKE_BUILD_TYPEset 命令显然采用“非字符串”参数。

变化

set(CMAKE_BUILD_TYPE 'Release')

set(CMAKE_BUILD_TYPE Release)

为我解决了这个问题。我首先深入研究了缓存系统,以了解为什么您的设置不起作用并且没有原因。我在 ccmake 中找到了一个 CMAKE_Fortran_FLAGS_'Release' 条目,并再次查看了文档中的变量 CMAKE_BUILD_TYPE,它采用值 empty, Debug, Release, RelWithDebInfo, MinSizeRel(我理解为未引用)。

【讨论】:

  • Explicit BUILD_TYPE to Release 确实使其行为正常。有什么理由吗?我的意思是我在 Cmake 文件的第 5-9 行中将构建类型设置为 Release。显式构建类型会有什么不同?以及关于如何实现自动化的任何建议?
  • 我的错,解决方案更简单。更新中。
  • 是的,这显然是个问题。我不知道为什么,因为我一直在另一台机器上使用它,并且我相信它也可以使用字符串(我将它作为项目导入 CLion 并且它正确地获得了所有标志)。并且我确信我只是从 SO 的一些接受的答案中挑选出来的!无论如何,谢谢你的回答
猜你喜欢
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
相关资源
最近更新 更多