在Mac OS X Mojave上使用AppleClang对OpenMP进行编译和链接 [英] Compiling and linking against OpenMP with AppleClang on Mac OS X Mojave
问题描述
我一直在尝试使用 CLion
IDE.
I've been trying to compile a simple OpenMP
program using AppleClang
on Mac OS X 10.14.5 Mojave
with the CLion
IDE.
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
构建项目时,出现链接器错误:
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
这怎么不起作用?使用open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
安装库头之后,可以从命令行成功运行clang++ main.cpp -lomp
.
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
.
message(STATUS "Linker flags:" "${OpenMP_EXE_LINKER_FLAGS}")
打印:
-- Linker flags:
如果我将CMAKE_EXE_LINKER_FLAGS
设置器替换为具有链接功能的set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp")
编译器.为什么我必须手动指定?使它以独立于平台的方式工作的首选方式是什么? -fopenmp
产生clang: error: unsupported option '-fopenmp'
. Linux
和Windows
上的gcc
和MSVC
在CMake OpenMP
配置下分别表现不错,但Mac OS X
则不然.
If I replace the CMAKE_EXE_LINKER_FLAGS
setter with set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp")
compilation with linking works. 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'
. gcc
and MSVC
on Linux
and Windows
respectively played nice with the CMake OpenMP
configuration but Mac OS X
does not.
推荐答案
如解决方案 Tsyvarev
所述,是使用更新"的方式,其中包括
As commented by Tsyvarev
, the solution is to use the "updated" way of including OpenMP
in CMake
:
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++
此文件分别使用平台的默认编译器在Windows
,Ubuntu
和Mac OS X
上编译.
This compiled on Windows
, Ubuntu
and Mac OS X
using their platform's default compilers respectively.
推荐的答案在这里,尽管它也有最多的支持,但不建议这样做.
The accepted answer here is not recommended even though it also has the most upvotes.
这篇关于在Mac OS X Mojave上使用AppleClang对OpenMP进行编译和链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!