Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake command line for C++ #define

I need to compile different versions of a certain project by adding compiler switches. Usually I would do this by using add_definitions or something like

set_property( TARGET mylib PROPERTY COMPILE_DEFINITIONS _MYDEFINE=1 ) 

in the CMakeLists.txt file.

In this specific project however, I am not allowed to modify any sources, including the CMakeLists.txt file.

I was hoping that something like

cmake -D_MYDEFINE=1 <path to sources> 

would generate a project file (Visual Studio 2008 in my case, but shouldn't matter) which includes _MYDEFINE=1 in its preprocessor definitions but in fact it won't.

What are my options here? Is there a different cmake command line option to achieve this? Feel free to suggest solutions not including the command line, as long as changing the project's CMakeLists.txt is not necessary.

like image 781
Tim Meyer Avatar asked Mar 02 '12 10:03

Tim Meyer


People also ask

Is CMake used for C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).

How use CMake command line?

Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

What is the CMake command?

The cmake command creates many files at your current working directory (CWD, the directory you ran the command from), and among them is a file called Makefile , which has rules about which files to compile/build/copy/write/whatever and how to do it.


1 Answers

I managed to do it this way now:

I was able to convince everybody to add the following lines to the common CMakeLists.txt:

IF (NOT DEFINED _MYDEFINE)     SET(_MYDEFINE <default value>) ENDIF() ADD_DEFINITIONS(-D_MYDEFINE=${_MYDEFINE}) 

(No it is not really called "MYDEFINE", and <default value> is just a placeholder, I just replaced all that for this example)

This does not change the current behaviour of compiling with no additional compiler flags and is thus a valid change.

And it allows you to do

cmake -D_MYDEFINE=<my value> <path to sources> 

where this cmake definition will be mapped to a C++ precompiler definition when cmake creates the project file.

like image 51
Tim Meyer Avatar answered Sep 18 '22 09:09

Tim Meyer