Skip to content

FindOpenMP: unable to detect OpenMP on windows with MSVC+LLVM

I have a simple CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 3.6)

PROJECT(test_openmp CXX)
find_package(OpenMP)
message(">> ${OpenMP_FOUND} ${OpenMP_VERSION}")
add_executable(parallel parallel_region.cpp)

As indicated, I'd love to generate build files for some OpenMP code with cmake. The source code file looks like this:

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

int main(int argc, char *argv[])
{
	
  int result = 0;
#pragma omp parallel
  {
    int ID = omp_get_thread_num();
#pragma omp critical
    {
      std::cout << "adding " << ID << "\n";
      result += ID;
    }
  }

  #ifdef _OPENMP
  std::unordered_map<unsigned, std::string> map{
	  {200505,"2.5"},{200805,"3.0"},{201107,"3.1"},{201307,"4.0"},{201511,"4.5"} };
  std::cout << int(_OPENMP) << " " << "(OpenMP " << map.at(_OPENMP).c_str() << ") ";
  #endif
  std::cout << " n_threads = " << omp_get_num_threads() << " result is " << result << "\n";
  return 0;
}

I am successful in running cmake with MSVC 15 2017:

$ cmake -Ax64 ..
Re-run cmake no build system arguments
-- Building for: Visual Studio 15 2017
-- The CXX compiler identification is MSVC 19.16.27030.1
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_CXX: -openmp (found version "2.0")
-- Found OpenMP: TRUE (found version "2.0")
>> TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/steinbac/development/cmake_sandbox/find_openmp_vs_llvm/build

My ultimate goal is to use openmp shipped with LLVM/clang 8.0.0 and higher. So, I installed llvm and ran cmake:

$ cmake -Tllvm -Ax64 ..
Re-run cmake no build system arguments
-- Building for: Visual Studio 15 2017
-- The CXX compiler identification is Clang 8.0.0 with MSVC-like command-line
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
-- Could NOT find OpenMP (missing: OpenMP_CXX_FOUND)
>> FALSE
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/steinbac/development/cmake_sandbox/find_openmp_vs_llvm/build

The subsequent call to cmake --build . fails due to linker errors. Building the source code file by hand works:

$ clang++.exe -fopenmp parallel_region.cpp
parallel_region-75f09d.o : warning LNK4217: locally defined symbol __std_terminate imported in function "?dtor$6@?0?.omp_outlined.@4HA" (?dtor$6@?0?.omp_outlined.@4HA)
parallel_region-75f09d.o : warning LNK4217: locally defined symbol _CxxThrowException imported in function "public: void __cdecl std::ios_base::clear(int,bool)" (?clear@ios_base@std@@QEAAXH_N@Z)

The produced executable runs perfectly fine. I am wondering why cmake cannot set up the right flags for clang.

Edited by Brad King
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information