[CMake] link errors using OpenMP

Nils Gladitz gladitz at sci-vis.de
Tue Oct 9 03:03:01 EDT 2012


At least for gcc I think you can fix this by passing 
"${OpenMP_CXX_FLAGS}" to the linker as well (e.g. LINKER_FLAGS target 
property).
-fopenmp at the link stage will instruct gcc to pass the appropriate 
libraries to the linker.

Nils

On 10/08/2012 11:30 PM, Matthew Woehlke wrote:
> 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.
>


-- 
Nils Gladitz, B.Sc.
DICOM, Konnektivität und Entwicklung

Scivis wissenschaftliche Bildverarbeitung GmbH
Bertha-von-Suttner-Str. 5
D-37085 Göttingen
GERMANY
Handelsregister Nr. / Trade Register No. B3100 Göttingen
Geschäftsführer / Managing Directors Dr. Gernot Ebel, Dr. Uwe Engeland

Tel: 0049 (0)551 634181-28
E-Mail: gladitz at scivis.de
Web: www.scivis.de



More information about the CMake mailing list