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

Ryan Pavlik rpavlik at iastate.edu
Tue Aug 31 07:10:07 EDT 2010


  On 8/31/10 3:40 AM, Raymond Wan wrote:
> 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
My guess is that you're generating your config.h before (earlier in the 
file) you are setting the HAVE_OPENMP variable, because everything else 
you've mentioned sounds OK.

Ryan

-- 
Ryan Pavlik
Human-Computer Interaction Graduate Student
Virtual Reality Applications Center
Iowa State University

http://academic.cleardefinition.com/



More information about the CMake mailing list