rbaleksandar
rbaleksandar

Reputation: 9691

Combining CMake option() with add_definitions()

Currently I have the following in my CMakeLists.txt (only the essential part for this question is listed below):

option(NORMALS_WITH_OPENMP "Enable OpenMP for estimating the normals of a point cloud" OFF)
if(NORMALS_WITH_OPENMP)
  message("OpenMP enabled")
  add_definitions(-DENABLE_OPENMP)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif()

In one of my source files I have:

#ifdef ENABLE_OPENMP
  #include <pcl/features/normal_3d_omp.h>
#else
  #include <pcl/features/normal_3d.h>
#endif

and other places in the code in that source file are also enabled/disabled in the same manner based on whether ENABLE_OPENMP has been defined or not.

My requirement for this is to be able give to brief description about the OpenMP support (using option()) and then if the option is set to ON add the ENABLE_OPENMP definition so that my code is compiled differently (using add_definitions()).

Questions:

Thanks!

Upvotes: 1

Views: 2280

Answers (1)

arrowd
arrowd

Reputation: 34401

If you use OpenMP only for this NORMALS feature, it's ok. Otherwise, you actually mixing two things - OpenMP usage and using it for NORMALS. Had you more optional features implemented with OpenMP, you should've do something like that:

find_package(OpenMP)

# enable OpenMP if compiler supports it
if(OpenMP_FOUND)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

# let user choose each option if OpenMP is there
if(OpenMP_FOUND)
  option(NORMALS_WITH_OPENMP "Blabla" OFF)
  option(ANOTHER_THING_WITH_OPENMP "Blabla" ON)
endif()

Now you can either conditionally use add_definitions(-DFEATURE) or, as @Tsyvarev suggested, create an config.h.cmake file with lines

#cmakedefine NORMALS_WITH_OPENMP
#cmakedefine ANOTHER_THING_WITH_OPENMP

and configure_file() it, handling all definitions at once.

Upvotes: 1

Related Questions