Cmake Bug

1 minute read

Cmake can not find OpenMP

  1. Clion(2021.2.2) use the default Cmake version is 3.20.2
    1. Got following error at path-to-build/CMakeFiles/CMakeError.log

        -- The C compiler identification is MSVC 19.29.30133.0
        -- The CXX compiler identification is MSVC 19.29  30133.0
        -- Detecting C compiler ABI info
        -- Detecting C compiler ABI info - failed
        -- Check for working C compiler: C:/Program Files  (x86)/Microsoft Visual Studio/2019/BuildTools/VC  Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe
        -- Check for working C compiler: C:/Program Files  (x86)/Microsoft Visual Studio/2019/BuildTools/VC  Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe -  works
        -- Detecting C compile features
        -- Detecting C compile features - done
        -- Detecting CXX compiler ABI info
        -- Detecting CXX compiler ABI info - failed
        -- Check for working CXX compiler: C:/Program  Files (x86)/Microsoft Visual Studio/2019  BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64  x64/cl.exe
        -- Check for working CXX compiler: C:/Program  Files (x86)/Microsoft Visual Studio/2019  BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64  x64/cl.exe - works
        -- Detecting CXX compile features
        -- Detecting CXX compile features - done
      
  2. After a few tests, I have a status table at below, we can follow this issue
  3.   PC1[xps] PC2[ucas]
    clion(cmake 3.20.2)
    cmake 3.21.3
    cmake 3.19.8 ✅warning(CMAKE_DEPENDS_USE_COMPILER)
  4. change clion use cmake 3.19.8, the problem has gone

How to use OpenMp

  • Need some attention code at CMakeLists.txt
  • CMakeLists.txt

    FIND_PACKAGE(OpenMP)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    
  • Using OMP_NUM_THREADS=4 at env To Control thread num

Code, A simple example

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

int main() {
    int i;
    #pragma omp parallel for private(i)
    for(i = 0;i<10;i++){
        std::cout << "Hello, World!" << i << "\t" <<omp_get_thread_num() << std::endl;
    }
    return 0;
}

Reference

Categories:

Updated: