[CMake] Conditional compiling [i.e. OpenMP]

Raymond Wan r.wan at aist.go.jp
Tue Aug 31 04:40:39 EDT 2010


Hi all,

I'm still quite new to cmake and am trying to do something I use to do
with autotools (took me ages to figure that out :-) ).  I guess what
I'm trying to do is not specific to OpenMP, though.

In my C++ code, I'd like to add:

#if HAVE_OPENMP
#include <omp.h>
#endif

and have this check around other OpenMP-related lines.  I came across
this article on "How to write platform checks":
http://www.cmake.org/Wiki/CMake:How_To_Write_Platform_Checks .

So, I tried to do something similar (but with SET since the article
gave an example on checking for header files which I don't need to
do), and it doesn't seem to work.  For example, I have this:

#####
FIND_PACKAGE (OpenMP)

if (OPENMP_FOUND)
  SET (HAVE_OPENMP 1)
  SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
#####

and then created a "config.hpp.in" which had this:

#cmakedefine HAVE_OPENMP 1

config.hpp gets generated fine, but it ends up having this:

/* #undef HAVE_OPENMP */

I can confirm that OpenMP is being detected because I can see the
various values being defined in CMakeCache.txt.  HAVE_OPENMP is not in
this file, though (it should, I presume?).

I think "SET (HAVE_OPENMP 1)" is wrong, but I don't know what should
it be.  If it is correct, do I need to initialize it with "SET
(HAVE_OPENMP 0)" before?

Thank you!

Ray


More information about the CMake mailing list