在 Mac OS X Mojave 上使用 AppleClang 针对 OpenMP 进行编译和链接

Compiling and linking against OpenMP with AppleClang on Mac OS X Mojave

我一直在尝试使用 Mac OS X 10.14.5 Mojave 上的 AppleClangCLion IDE.

编译一个简单的 OpenMP 程序

main.cpp:

#include <omp.h>
#include <iostream>

int main() {
    std::cout << omp_get_max_threads() << std::endl;
    return 1;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)

set(CMAKE_CXX_STANDARD 17)

add_executable(OpenMPTest main.cpp)

find_package(OpenMP)
if (OPENMP_FOUND)
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    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 输出:

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/bully/CLionProjects/OpenMPTest
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found version "3.1") 
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1") 
-- Found OpenMP: TRUE (found version "3.1")  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug

当我构建项目时,我收到一个链接器错误:

====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug --target all -- -j 2
Scanning dependencies of target OpenMPTest
[ 50%] Building CXX object CMakeFiles/OpenMPTest.dir/main.cpp.o
[100%] Linking CXX executable OpenMPTest
Undefined symbols for architecture x86_64:
  "_omp_get_max_threads", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [OpenMPTest] Error 1
make[1]: *** [CMakeFiles/OpenMPTest.dir/all] Error 2
make: *** [all] Error 2

这怎么行不通?在使用 open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg.

安装库头文件后,我可以从命令行成功 运行 clang++ main.cpp -lomp

message(STATUS "Linker flags:" "${OpenMP_EXE_LINKER_FLAGS}") 打印:

-- Linker flags:

如果我将 CMAKE_EXE_LINKER_FLAGS setter 替换为带有链接作品的 set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp") 编译。为什么我必须手动指定它?使它以独立于平台的方式工作的首选方法是什么? -fopenmp 产生 clang: error: unsupported option '-fopenmp'。在 LinuxWindows 上的 gccMSVC 分别与 CMake OpenMP 配置配合得很好,但 Mac OS X 没有。

正如 Tsyvarev, the solution is to use the "updated" way of including OpenMPCMake 中评论的那样:

cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)

set(CMAKE_CXX_STANDARD 17)

add_executable(OpenMPTest main.cpp)

find_package(OpenMP REQUIRED) # Find the package
target_link_libraries(${PROJECT_NAME} ${OpenMP_CXX_LIBRARIES}) # Link against it for C++

这分别使用其平台的默认编译器在 WindowsUbuntuMac OS X 上编译。

不推荐已接受的答案 here,尽管它也有最多的赞成票。