4

I'm working with an existing project and cleaning up the CMake for it. However, right now I'm a bit confused by how exactly to go about integrating the CMake options into the actual source code.

For simplicity's sake, let's say I'd like to only execute one chunk of code, let's say cout << "print";, inside example.cpp if, on CMake, the value ENABLE_PRINT is set to ON.

The project directory will look like this: folder layout

Using the above example, I did the following:

  1. On the parent project CMakeLists.txt, I added OPTION( ENABLE_PRINT "Enable Print" ON)
  2. Then, on the example subproject source folder Config.h file, I added #define ENABLE_PRINT
  3. On the Config.h.in located in the example subproject, I added #cmakedefine ENABLE_PRINT
  4. Finally, on the source file example.cpp, I encircled cout << "print"; inside #ifdef ENABLE_PRINT and #endif

After making those changes, the project will configure and generate just fine. However, when I make the software, it will error and essentially tell me that the chunk of code I encircled with #ifdef was not executed at all; it was ignored. In other words, the above steps I took did not do anything except "comment out" the chunk of code I wanted to make conditional upon ENABLE_PRINT.

So, how would I make this work?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
assignment_operator
  • 1,213
  • 4
  • 12
  • 14
  • Could you provide actual generated cpp file, generated config.h file and the actual compiler(?) error message? – Artem Tokmakov Jul 09 '14 at 20:40
  • I'm not sure I get what you're asking for, sorry. Do you mean the actual files or whatever CMake will generate from them? If you mean the actual files: the project is pretty large and that would be VERY difficult. However, my example project up there is pretty much a simplified replica of what's going on. One of the errors is: `../vlApplication/libvlapp.a(vlMPIApplication.cpp.o): In function 'VLAPP::MPIApplication::draw()': vlMPIApplication.cpp:(.text+0x3043): undefined reference to 'VLUTIL::Timers::MPITimerManager::syncMultiTimers()` – assignment_operator Jul 09 '14 at 21:02
  • i.e. (as far as I can tell), it could not find the function syncMultiTimers() which is something I have in between my `#ifdef` – assignment_operator Jul 09 '14 at 21:03
  • If this does repro with your simplified project, that's good enough. I'd want to see example.cpp and Config.h which you mention in your question (assuming that you actually have this small repro project). Now, the error you're giving is indeed saying that method is missing. Is this the case when you expect it to be there, i.e. is option "on" in this case? Are you sure your ifdefs and macros are correct? – Artem Tokmakov Jul 09 '14 at 21:15

2 Answers2

9

You may combine option and add_definitions of cmake like here. Since a simple example is clearer than a long text here is a little main.c :

 #include<stdio.h>

 int main(int argc, char *argv[])
 {
   printf("start\n");
   #ifdef USE_DEBUG
     printf("Using debug\n");
   #endif
   printf("end\n");
   return 0;
 }

The CMakeLists.txt is :

cmake_minimum_required (VERSION 2.6)
project (HELLO) 

option(WITH_DEBUG "Use debug" OFF)

if (WITH_DEBUG)
  MESSAGE(STATUS "WITH_DEBUG")
  add_definitions(-DUSE_DEBUG)
endif()

add_executable (main main.c) 

You can try it by typing cmake . or cmake . -DWITH_DEBUG=ON then make and ./main

Community
  • 1
  • 1
francis
  • 9,525
  • 2
  • 25
  • 41
1

@francis answer is good, however I would also recommend using compile_definitions instead of add_definitions as well as specifying target for modern cmake modularity:

cmake_minimum_required (VERSION 2.6)
project (HELLO) 

option(WITH_DEBUG "Use debug" OFF)

add_executable (main main.c) 

if (WITH_DEBUG)
  MESSAGE(STATUS "WITH_DEBUG")
  target_compile_definitions(main PRIVATE USE_DEBUG)
endif()
greygore
  • 66
  • 5