0

System info

  1. macos 12.5.1
  2. Compiler Apple clang 14.0.0
  3. Cmake version 3.24.2 from brew
  4. OpenMP version 15.0.7 from brew, this installs the library in /opt/homebrew/opt/libomp/

The problem

I am debugging an issue where OpenMP cannot be found by cmake, but it does not make sense and I must be missing something obvious.

Assume initially this CMakeLists.txt

# attempt 1
cmake_minimum_required(VERSION 3.24)
project(openmp-bug)
find_package(OpenMP REQUIRED)

Running rm -rf * && cmake .. inside build/, results in OpenMP not being found

> rm -rf * && cmake ..
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /opt/homebrew/Cellar/cmake/3.24.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
Call Stack (most recent call first):
  /opt/homebrew/Cellar/cmake/3.24.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /opt/homebrew/Cellar/cmake/3.24.2/share/cmake/Modules/FindOpenMP.cmake:545 (find_package_handle_standard_args)
  CMakeLists.txt:20 (find_package)


-- Configuring incomplete, errors occurred!
See also "/Users/rasmus/Desktop/cmake-openmp/build/CMakeFiles/CMakeOutput.log".
See also "/Users/rasmus/Desktop/cmake-openmp/build/CMakeFiles/CMakeError.log".

Seeing that OpenMP headers and libraries are in a nonstandard location, this is not surprising. So I'm thinking to just find the installation path on osx and inform cmake:

# attempt 2
cmake_minimum_required(VERSION 3.24)
project(openmp-bug)
execute_process(
    COMMAND brew --prefix libomp
    ERROR_QUIET
    OUTPUT_VARIABLE OPENMP_PREFIX
    RESULT_VARIABLE find-openmp-status
    )
if (find-openmp-status EQUAL 0)
    message(STATUS "OpenMP prefix: ${OPENMP_PREFIX}")
    list(APPEND CMAKE_PREFIX_PATH ${OPENMP_PREFIX})
else ()
    message(
        FATAL_ERROR
        "Could not find OpenMP prefix Status was ${find-qt-status}"
        )
endif ()

find_package(OpenMP REQUIRED)

But this for some reason fails with the identical output:

> rm -rf * && cmake ..
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- OpenMP prefix: /opt/homebrew/opt/libomp    <--- !!!!!! note path is found here

CMake Error at /opt/homebrew/Cellar/cmake/3.24.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
Call Stack (most recent call first):
  /opt/homebrew/Cellar/cmake/3.24.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /opt/homebrew/Cellar/cmake/3.24.2/share/cmake/Modules/FindOpenMP.cmake:545 (find_package_handle_standard_args)
  CMakeLists.txt:19 (find_package)


-- Configuring incomplete, errors occurred!
See also "/Users/rasmus/Desktop/cmake-openmp/build/CMakeFiles/CMakeOutput.log".
See also "/Users/rasmus/Desktop/cmake-openmp/build/CMakeFiles/CMakeError.log".

Now if I hardcode the path, it works, even though the hardcoded path is identical to the computed one:

# attempt 3
cmake_minimum_required(VERSION 3.24)
project(openmp-bug)
list(APPEND CMAKE_PREFIX_PATH /opt/homebrew/opt/libomp)
find_package(OpenMP REQUIRED)

Is there some invisible quoting going on here or what could the issue be?

oarfish
  • 4,116
  • 4
  • 37
  • 66
  • Most likely the variable `OPENMP_PREFIX` contains a **trailing newline**. You may avoid it by using option `OUTPUT_STRIP_TRAILING_WHITESPACE` in `execute_process` call. BTW, printing a variable's value with surrounding quotes will help to easily detect artificial characters: `message(STATUS "OpenMP prefix: '${OPENMP_PREFIX}'")` – Tsyvarev Mar 15 '23 at 11:16
  • Oh my god, that is it. Seems obvious in hindsight. Care to make this an answer? – oarfish Mar 15 '23 at 11:28
  • I don't think that Question/Answer library of Stack Overflow will be benefit from answering every question about unnoticed trailing newline. (You are not alone: https://stackoverflow.com/questions/71532091). So I just marked your question as a duplicate of the one about removing that trailing newline. Among other answers it has [the one](https://stackoverflow.com/a/42452019/3440745) about specifically `execute_process`. Having a duplicate doesn't mean that your question is bad. Moreover, your title seems to be quite good in attracting people encountered a problem of that kind. – Tsyvarev Mar 15 '23 at 11:50

0 Answers0