简体   繁体   English

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

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

I've been trying to compile a simple OpenMP program using AppleClang on Mac OS X 10.14.5 Mojave with the CLion IDE. 我一直在试图编译一个简单OpenMP使用程序AppleClangMac OS X 10.14.5 MojaveCLion IDE。

main.cpp : main.cpp

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

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

CMakeLists.txt : 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 output: 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

When I build the project, I receive a linker error: 生成项目时,出现链接器错误:

====================[ 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

How come this does not work? 为什么这不起作用? From the command line I can run clang++ main.cpp -lomp successfully after installing the library headers with open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg . open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg安装库头文件之后,可以clang++ main.cpp -lomp成功运行clang++ main.cpp -lomp

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

-- Linker flags:

If I replace the CMAKE_EXE_LINKER_FLAGS setter with set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp") compilation with linking works. 如果我将CMAKE_EXE_LINKER_FLAGS设置器替换为set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp")通过链接进行编译。 Why do I have to specify this manually? 为什么我必须手动指定? What's the preferred way of getting this to work in a platform-independent way? 使它以独立于平台的方式工作的首选方式是什么? -fopenmp yields clang: error: unsupported option '-fopenmp' . -fopenmp产生clang: error: unsupported option '-fopenmp' gcc and MSVC on Linux and Windows respectively played nice with the CMake OpenMP configuration but Mac OS X does not. LinuxWindows上的gccMSVC分别与CMake OpenMP配置配合使用很不错,而Mac OS X则不然。

As commented by Tsyvarev , the solution is to use the "updated" way of including OpenMP in CMake : 正如Tsyvarev所评论的Tsyvarev ,解决方案是使用CMake中包含OpenMP“更新”方式

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++

This compiled on Windows , Ubuntu and Mac OS X using their platform's default compilers respectively. 分别使用其平台的默认编译器在WindowsUbuntuMac OS X进行了编译。

The accepted answer here is not recommended even though it also has the most upvotes. 即使推荐最多,也不建议在这里接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM