铿锵声、openMP 和 CMake

Posted

技术标签:

【中文标题】铿锵声、openMP 和 CMake【英文标题】:clang, openMP and CMake 【发布时间】:2016-10-24 11:29:12 【问题描述】:

我在使用 cmake 构建简单的 openMP 代码时遇到问题。

我已经安装了clang-omp,我可以用clang-omp -fopenmp filename.c编译程序

我做了很多尝试来制作 CMakeLists.txt 文件,但没有结果! 在我包含的文件中:

FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $OpenMP_CXX_FLAGS")
set(CMAKE_EXE_LINKER_FLAGS "$CMAKE_EXE_LINKER_FLAGS $OpenMP_EXE_LINKER_FLAGS")
endif()

我尝试通过调用来告诉 CMake 使用该编译器:

CC=clang-omp CXX=clang-omp++ cmake 

没有。

两种情况的结果如下:

-- Try OpenMP C flag = [-fopenmp=libomp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [ ]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [/openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-Qopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-xopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [+Oopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-qsmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-mp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-fopenmp=libomp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [ ]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [/openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-Qopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-xopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [+Oopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-qsmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-mp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find OpenMP (missing: OpenMP_C_FLAGS OpenMP_CXX_FLAGS)
Call Stack (most recent call first):
  /Applications/CMake.app/Contents/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
  /Applications/CMake.app/Contents/share/cmake-3.5/Modules/FindOpenMP.cmake:234 (find_package_handle_standard_args)
  CMakeLists.txt:4 (FIND_PACKAGE)

【问题讨论】:

你确定 CMake 从环境变量中获取编译器吗?根据FAQ,这样的编译器设置并不总是有效。 cmake -DCMAKE_CXX_COMPILER=clang-omp++ -DCMAKE_C_COMPILE=clang-omp 怎么样? 你是如何安装clang的,什么版本?最近,openmp 支持比以前更精简,不用担心太多。话虽如此,与 GCC 不同,您需要安装 openmp 运行时库。检查你的包管理器。 正如 yougmit 所说,Linux 上的 Clang 3.8 在编译器中包含 OpenMP 支持,因此无需使用特殊版本。它还包括 OpenMP 运行时,所以一切都应该“正常工作” @Jim Cownie,不幸的是,使用 clang 3.8 它不能“正常工作”。我收到与提问者完全相同的错误消息。运行时安装在哪里? 【参考方案1】:

添加非系统 clang(对我来说是 LLVM 4.0)的问题是您必须添加 include 和 lib 目录。 llvm-config 在我的情况下正确选择了它们,但 CMake 没有使用它们。

假设您的PATH 没问题,并且您已经设置了CCCXX,您需要添加:

export C_INCLUDE_PATH=$(llvm-config --includedir)
export CPLUS_INCLUDE_PATH=$(llvm-config --includedir)
export LIBRARY_PATH=$(llvm-config --libdir)

如果你更喜欢 Lmod,这个 .lua 文件应该可以做到:

help([[
This is the module file for the llvm and clang compiler.
]])

local version = "4.0"

whatis("Name: LLVM")
whatis("Version: " .. version)
whatis("Keywords: System, Compiler")
whatis("URL: http://www.llvm.org/")
whatis("Description: LLVM compiler family")

family("compiler")

local prefix = "/work/llvm"

prepend_path("PATH", pathJoin(prefix, "bin"))
setenv("CC",  pathJoin(prefix, "bin/clang"))
setenv("CXX", pathJoin(prefix, "bin/clang++"))
setenv("C_INCLUDE_PATH", pathJoin(prefix, "include"))
setenv("CPLUS_INCLUDE_PATH", pathJoin(prefix, "include"))
setenv("LIBRARY_PATH", pathJoin(prefix, "lib"))
prepend_path("DYLD_LIBRARY_PATH", pathJoin(prefix, "lib"))

(对于 Mac,将 DYLD 更改为 LD 对于 Linux)

这在 CMake 上工作得很好,它接收 -fopenmp。手动编译也只需要一个标志。

【讨论】:

以上是关于铿锵声、openMP 和 CMake的主要内容,如果未能解决你的问题,请参考以下文章

我如何让铿锵声转储没有颜色的 AST?

铿锵声:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)而实现?

如何从GCC /铿锵声组件输出中消除“噪音”?

_mm_cvtsi64x_si128 的铿锵声支持

conflict , clash ,collision 表示冲突、矛盾的时候有啥区别?

未初始化的价值和铿锵优化