4

I'm trying to compile a simple app to test a few libraries I might be using in the future. Because of some problems I had with msvc I tried Clang, which made a strange error I got disappear.

The problem I have now is that the libraries I want to test use OpenMP. They import it using the FindOpenMP module CMake privides. However the module doesn't find it with Clang.

cmake_minimum_required(VERSION 3.14.0)
project(blaze-test VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenMP)

I got this output :

1> CMake generation started for configuration: 'x64-Debug'.
1> Environment settings:
1>     CXXFLAGS=-m64 -fdiagnostics-absolute-paths
1>     CFLAGS=-m64 -fdiagnostics-absolute-paths
1> Command line: "cmd.exe" /c ""C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe"  -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="PATH\blaze-test\install\x64-Debug" -DCMAKE_CXX_COMPILER:FILEPATH="C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe" -DCMAKE_C_COMPILER:FILEPATH="C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe"  -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "PATH\blaze-test" 2>&1"
1> Working directory: PATH\blaze-test\build\x64-Debug
1> [CMake] -- The C compiler identification is Clang 8.0.0
1> [CMake] -- The CXX compiler identification is Clang 8.0.0
1> [CMake] -- Check for working C compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe
1> [CMake] -- Check for working C compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe -- works
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Check for working CXX compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe
1> [CMake] -- Check for working CXX compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe -- works
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES) 
1> [CMake] -- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES) 
1> [CMake] -- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)  

Based on this I added

set(OpenMP_CXX_FLAGS "-Xclang -fopenmp" CACHE STRING "" FORCE)
set(OpenMP_C_FLAGS "-Xclang -fopenmp" CACHE STRING "" FORCE)

right before the find_package call. It removed the part about the compiler flags from the error message. I added libomp.lib the same way. Then I get

1> [CMake] -- Could NOT find OpenMP_C (missing: OpenMP_C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/8.0.0/lib/libomp.lib_LIBRARY) 
1> [CMake] -- Could NOT find OpenMP_CXX (missing: OpenMP_C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/8.0.0/lib/libomp.lib_LIBRARY) 
1> [CMake] -- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)

I hope someone can tell me how to get CMake to find it. These unreliable Find Modules are really annoying.

ps: I'm using Clang 8 (clang-cl.exe) with CMake 3.14 and Visual Studio 2019.

Thorsten Schmitz
  • 542
  • 7
  • 19
  • Have you tried [that answer](https://stackoverflow.com/a/48867837/3440745)? – Tsyvarev Jul 25 '19 at 22:30
  • I tried. Adding the compiler flags worked, but CMake complains about missing libraries. It sais to add libomp.lib to the link libraries, but that doesn't solve the issue. IT doesn't seem to accept the lib. The error message changes from "missing: OpenMP_CXX_LIB_NAMES" to "missing: OpenMP_C:/Program Files/LLVM/lib/libomp.lib_LIBRARY" – Thorsten Schmitz Jul 25 '19 at 23:10
  • Please, add description of your attempt to the **questions post**. With exact command line you use and **exact error message**. Your current description is too vague for us to help you. – Tsyvarev Jul 25 '19 at 23:16

1 Answers1

1

When using find_package(OpenMP REQUIRED), I have also received similar errors when using clang-cl from Visual Studio. There are known limitations with CMake's FindOpenMP.cmake module, as well as some lack of support the clang-cl side. There are other answers on this site suggesting to populate the CMake OpenMP_ variables manually, but this seems backwards. When I tried it, CMake claimed that OpenMP was FOUND, but the paths to the libraries was still unknown and compilation was unsuccessful. I was able to successfully run CMake then compile my applications with MSVC+Clang by avoiding the use of find_package and (unfortunately) hard-coding the path to the OpenMP libraries for now:

cmake_minimum_required(VERSION 3.14.0)
project(blaze-test VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(OpenMP_LIBRARY_DIR "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/lib")
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")

# Tell CMake where to find the OpenMP libraries.
link_directories(${OpenMP_LIBRARY_DIR})

# Library
add_library(MyLibrary SHARED MyExample.h MyExample.cpp)
# Link in the OpenMP libraries.
target_link_libraries(MyLibrary PUBLIC libomp libiomp5md)

# Executable
add_executable(MyOpenMpTest MyOpenMpTest .cpp)
target_link_libraries(MyOpenMpTest MyLibrary)

Hopefully, the folks at CMake and LLVM will get this issue worked out soon and FindOpenMP.cmake can be used more reliably.

Kevin
  • 16,549
  • 8
  • 60
  • 74