-1

I have managed to compile OpenMP on my macbook but it does not run the application on multiple threads. I used the answer here.

Here is my CMakeList.txt:

cmake_minimum_required(VERSION 3.12)

project(playground)

if(APPLE)
    set(CMAKE_C_COMPILER clang)
    set(CMAKE_CXX_COMPILER clang++)

    if(CMAKE_C_COMPILER_ID MATCHES "Clang\$")
        set(OpenMP_C_FLAGS "-Xpreprocessor -Xclang -fopenmp")
        set(OpenMP_C_LIB_NAMES "omp")
        set(OpenMP_omp_LIBRARY omp)
    endif()

    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$")
        set(OpenMP_CXX_FLAGS "-Xpreprocessor -Xclang -fopenmp")
        set(OpenMP_CXX_LIB_NAMES "omp")
        set(OpenMP_omp_LIBRARY omp)
    endif()

endif()

find_package(OpenMP REQUIRED)

add_executable(helloworld openmp.cpp)

set(OMPIncludeDirectory "/opt/homebrew/opt/libomp/include")
target_include_directories(helloworld PUBLIC ${OMPIncludeDirectory} )

target_link_libraries(helloworld PUBLIC /opt/homebrew/opt/libomp/lib/libomp.dylib)

This one compiles but the application doesn't run on multiple threads (only single).

If I don't explicitly provide the library path and instead change the last line to: target_link_libraries(helloworld PUBLIC OpenMP::OpenMP_CXX)

It gives me linkage error:

cmake --build build
[ 50%] Linking CXX executable helloworld
ld: library not found for -lomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [helloworld] Error 1
make[1]: *** [CMakeFiles/helloworld.dir/all] Error 2
make: *** [all] Error 2

I tried compiling by hand using the following command and it works. The executable also runs on multiple threads:

c++ openmp.cpp -I/opt/homebrew/opt/libomp/include -L/opt/homebrew/opt/libomp/lib/ -lomp -Xclang -fopenmp

Here is my simple test program:

// OpenMP program to print Hello World
// using C language

// OpenMP header
#include <omp.h>

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{

    // Beginning of parallel region
    #pragma omp parallel
    {

        printf("Hello World... from thread = %d\n",
            omp_get_thread_num());
    }
    // Ending of parallel region
}

What am I missing here? Please help.

Bibrak
  • 544
  • 4
  • 20
  • 1
    As instructed in [ask], can you please write a descriptive, non-ambiguous title? For more guidance, see [How do I write a good title?](//meta.stackexchange.com/q/10647/997587) – starball Apr 12 '23 at 23:17

1 Answers1

0

If you use

find_package(OpenMP REQUIRED)

you shouldn't spell out the other options explicitly:

target_link_libraries( program PUBLIC OpenMP::OpenMP_CXX)

(I"ve heard that Apple's native compiler may not come with OpenMP. I use gcc12 which is the most up-to-date as far as OpenMP features goes, and it's easy to get from a package manager such as macports or homebrew.)

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23
  • When I put `target_link_libraries(helloworld PUBLIC OpenMP::OpenMP_CXX)` it gives me linkage errors as shared in the post. – Bibrak Apr 12 '23 at 19:00
  • Could be that OpenMP is indeed missing from your installation. – Victor Eijkhout Apr 12 '23 at 19:20
  • 1
    I have seen similar problems with clang, OpenMP and CMake on linux-x86. CMake just doesn't seem to be able to handle clangs OpenMP implementation. – paleonix Apr 12 '23 at 23:48