MantisBT - CMake
View Issue Details
0009075CMakeModulespublic2009-05-27 16:462016-06-10 14:30
Torsten Rohlfing 
Bill Hoffman 
normalfeaturealways
closedmoved 
CMake-2-6 
 
0009075: FindOpenMP provides required C/CXX flags, not linker flags
According to CMake's man page section on "FindOpenMP":

The following variables are set:

   OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
   OpenMP_CXX_FLAGS - flags to add to the CXX compiler for OpenMP support
   OPENMP_FOUND - true if openmp is detected

However, to successfully build a binary, one must also activate OpenMP support during the linker stage (otherwise, unresolved symbols remain).

If CMake used the compiler front-end for linking, this could be achieved using the aforementioned C_FLAGS or CXX_FLAGS added to CMAKE_EXE_LINKER_FLAGS, but is there a guarantee that a separate linker, e.g., ld, will always accept the same flag as the compiler?

In a situation where the linker wants different flags for OpenMP support, there does not seem to be a way to completely configure the build using the information detected and provided by FindOpenMP (or the documentation is lacking in this respect).
No tags attached.
related to 0015393closed Kitware Robot undefined reference to `GOMP_parallel' 
Issue History
2009-05-27 16:46Torsten RohlfingNew Issue
2009-07-18 02:22Philip LowmanNote Added: 0016906
2009-07-18 02:22Philip LowmanAssigned To => Philip Lowman
2009-07-18 02:22Philip LowmanStatusnew => assigned
2009-08-29 21:35Philip LowmanAssigned ToPhilip Lowman =>
2009-08-29 21:35Philip LowmanSeveritymajor => feature
2009-08-29 21:35Philip LowmanStatusassigned => new
2009-09-11 17:28Bill HoffmanNote Added: 0017388
2009-09-11 17:28Bill HoffmanStatusnew => assigned
2009-09-11 17:28Bill HoffmanAssigned To => Bill Hoffman
2009-09-12 04:46Torsten RohlfingNote Added: 0017408
2010-06-25 01:41Ian CullinanNote Added: 0021163
2011-05-19 06:28Hanspeter PortnerNote Added: 0026548
2011-05-19 06:29Hanspeter PortnerNote Edited: 0026548bug_revision_view_page.php?bugnote_id=26548#r322
2013-10-03 04:27Matthäus G. ChajdasNote Added: 0033969
2013-10-03 04:32Matthäus G. ChajdasNote Deleted: 0033969
2015-02-09 11:21Clinton StimpsonRelationship addedrelated to 0015393
2016-06-10 14:27Kitware RobotNote Added: 0041562
2016-06-10 14:27Kitware RobotStatusassigned => resolved
2016-06-10 14:27Kitware RobotResolutionopen => moved
2016-06-10 14:30Kitware RobotStatusresolved => closed

Notes
(0016906)
Philip Lowman   
2009-07-18 02:22   
Hello,

I don't know a lot about OpenMP but I have commit access to Modules/ so I can fix any documentation errors at the bare minimum.

So your concern is that FindOpenMP doesn't work on certain compilers because linker flags may need to be added? Which compilers are causing problems?
(0017388)
Bill Hoffman   
2009-09-11 17:28   
We did test this with visual studio at some point. What compiler has this issue?
(0017408)
Torsten Rohlfing   
2009-09-12 04:46   
I think I saw this with SunStudio on Linux, but I currently no longer have any systems with this installed, so it will be a while before I can confirm with certainty.
(0021163)
Ian Cullinan   
2010-06-25 01:41   
I'm encountering this at the moment, trying to build a shared library (DLL) using OpenMP with GCC 4.4.0 (MinGW) on Windows. Doing

SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_CXX_FLAGS}")

is a workaround for now, but I doubt that that'll work with other toolchains.
(0026548)
Hanspeter Portner   
2011-05-19 06:28   
(edited on: 2011-05-19 06:29)
I have the same problem with GCC 4.5 on Linux x86 and cmake 2.8.4

gcc apps need to be linked with "-lgomp" (GNU OpenMP utility lib), if not there are undefined references.

when compiled and linked in one go, "-fopenmp" as compile flag is enough, and "-lgomp" is automatically linked in
-> gcc -fopenmp -o myapp myapp.c

but when compiled and linked in two steps (as does CMake), there is the need to either link in "-lgomp" manually or again give the compiler flag "-fopenmp"
-> gcc -fopenmp -c -o myapp.o myapp.c
-> gcc -o myapp myapp.o -lgomp OR gcc -fopenmp -o myapp myapp.o

this hack does the trick for gcc:
-> set_target_properties (myapp PROPERTIES COMPILE_FLAGS ${OpenMP_C_FLAGS})
-> set_target_properties (myapp PROPERTIES LINKER_FLAGS ${OpenMP_C_FLAGS})

but more elegant would be a new output variable ${OpenMP_LD_FLAGS} as output of FindOpenMP which can be set to either "-fopenmp" or "-lgomp" when gcc is detected or else to "" for the other compilers.

and then we could use this:
-> set_target_properties (myapp PROPERTIES COMPILE_FLAGS ${OpenMP_C_FLAGS})
-> set_target_properties (myapp PROPERTIES LINKER_FLAGS ${OpenMP_LD_FLAGS})

possible patch:

*** FindOpenMP.cmake 2011-05-19 12:18:30.000000000 +0200
--- FindOpenMP.cmake.gcc 2011-05-19 12:27:46.000000000 +0200
***************
*** 6,11 ****
--- 6,12 ----
  # The following variables are set:
  # OpenMP_C_FLAGS - flags to add to the C compiler for OpenMP support
  # OpenMP_CXX_FLAGS - flags to add to the CXX compiler for OpenMP support
+ # OpenMP_LD_FLAGS - flags to add to the linker for OpenMP support
  # OPENMP_FOUND - true if openmp is detected
  #
  # Supported compilers can be found at http://openmp.org/wp/openmp-compilers/ [^]
***************
*** 108,114 ****
--- 109,122 ----
  find_package_handle_standard_args(OpenMP DEFAULT_MSG
    OpenMP_C_FLAGS OpenMP_CXX_FLAGS )
  
+ if (OpenMP_C_FLAGS EQUAL "-fopenmp")
+ set (OpenMP_LD_FLAGS ${OpenMP_C_FLAGS})
+ else
+ set (OpenMP_LD_FLAGS "")
+ end (OpenMP_C_FLAGS EQUAL "-fopenmp")
+
  mark_as_advanced(
    OpenMP_C_FLAGS
    OpenMP_CXX_FLAGS
+ OpenMP_LD_FLAGS
  )

(0041562)
Kitware Robot   
2016-06-10 14:27   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.