When I write a simple program using OpenMP, with a CMakeLists.txt like:

find_package(OpenMP REQUIRED)
add_definitions(${OpenMP_CXX_FLAGS})
add_executable(foo foo.cpp)

I get link errors:

foo.cpp: undefined reference to `omp_get_num_threads'
foo.cpp: undefined reference to `omp_get_thread_num'
...etc.

This is caused by the .o containing references to OMP methods, but the linker does not link to the platform OpenMP library (e.g. libgomp.so for gcc; similar issues seen with icc).

Does anyone have a preferred fix for this?

Ideas:
- find the appropriate library, set OpenMP_LIBRARY
- add OpenMP_C[XX]_FLAGS to target's LINK_FLAGS property

For the moment at least, I've solved this in my own project by creating the following CMake function:

function(omp_add_flags TARGET LANG)
  if(OPENMP_FOUND)
    if(NOT LANG)
      get_target_property(LANG ${TARGET} LINKER_LANGUAGE)
    endif()
    if(LANG MATCHES "C(XX)?")
      set_property(TARGET ${TARGET} APPEND
                   PROPERTY COMPILE_FLAGS ${OpenMP_${LANG}_FLAGS})
      set_property(TARGET ${TARGET} APPEND
                   PROPERTY LINK_FLAGS ${OpenMP_${LANG}_FLAGS})
    else()
      message(WARNING "omp_add_flags: target '${TARGET}'"
                      " link language '${LANG}' is not supported")
    endif()
  endif()
endfunction()


This might need some tweaking, but if the general idea seems like the best way, I would propose adding this to FindOpenMP.cmake.

--
Matthew

--

Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake

Reply via email to