CMake command line for C++ #define

cmake preprocessor define command line
cmake define variable
cmake command line arguments
add_compile_definitions
cmake define build variable
cmake add_definitions not working
cmake ifdef
cmake define debug

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.

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.

add_definitions, Adds -D define flags to the compilation of source files. add_definitions(-DFOO -​DBAR ) Adds definitions to the compiler command line for sources in the current​  Run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool. Run the install step by using the install option of the cmake command (introduced in 3.15, older versions of CMake must use make install) from the command line, or build the INSTALL target from an IDE. This will install the

Container CMakeLists.txt solution

Tricky solution:

Your read only CMakeList.txt path: ${path}/ReadOnlyProject/CMakeLists.txt

Create a new CMakeList.txt to upper to the read only library (${path}/CMakeLists.txt):

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)
PROJECT (FAKE_PROJECT)

ADD_DEFINITIONS(-D_MYDEFINE=1)

ADD_SUBDIRECTORY(ReadOnlyProject)

Now use your new project (FAKE_PROJECT) to compile. If the ReadOnlyProject does not set compilers definitions directly, it could work.


On Visual Studio 2010:

Try to modify c:\Users\${username}\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props to add custom compiler settings.

You should add the followings:

<Project>
  ...
  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions>__MYDEFINE=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>
</Project>

cmake(1), Project configuration settings may be specified on the command line with the -D option. CMake is a -C <initial-cache>. Pre-load a script to populate If variables are defined using -D, this must be done before the -P argument. --find-​package  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_…

To pass a C++ or C pre-processor define without modifying any CMake source files, use the environment variables CFLAGS for C or CXXFLAGS for C++ respectively, e.g.:

$ export CXXFLAGS="-D_MY_DEFINE=1 -D_MY_OTHER_DEFINE=1"
$ mkdir build
$ cd build
$ cmake ..

Cross Platform Make, The only reason it is an option is to preserve compatibility with older CMake code​. ADD_DEFINITIONS : Adds -D define flags to the command line of C and C++  The initial content from the cached CMAKE_CXX_FLAGS variable is a combination of CMAKE_CXX_FLAGS_INIT set by CMake itself during OS/toolchain detection and whatever is set in the CXXFLAGS environment variable. So you can initially call: cmake -E env CXXFLAGS="-Wall" cmake ..

[CMake] How to add "-DDEBUG" just in command line?, ${CMAKE_BUILD_TYPE}) ENDIF(DEFINE_DEBUG) And ues the following command to add debug build: $ cmake -DDEFINE_DEBUG=ON  Now I'd like to use this CMake variable in my c++ source code too, but I actually don't get to a working solution. I've tried different things like this: #ifndef VERSION_LIBINTERFACE # define VERSION_LIBINTERFACE @LIBINTERFACE_VERSION@ #endif or this: unsigned int getLibInterfaceVersion() { return @LIBINTERFACE_VERSION@; }

Using CMake to set preprocessor directives, One use is to set flags from the command line in order to compile the program in different ways. Is equivalent to writing #define DEBUG 1 in main.cpp . our project: project(demo_cmake) # This allows us to enable C++11  The “cmake” executable is the CMake command-line interface. It may be used to configure projects in scripts. Project configuration settings may be specified on the command line with the -D option. CMake is a cross-platform build system generator.

CMake projects in Visual Studio, CMake is a cross-platform, open-source tool for defining build processes that development with C++ and Linux Development with C++ workloads. The CMake command line is displayed in the Output Window, along with  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.

Comments
  • ADD_DEFINITIONS(-D_MYDEFINE)
  • Hm my %APPDATA%\Microsoft folder doesn't have any MSBuild folder (I'm on Windows XP btw)
  • Ok, I am using Visual Studio 2010. I will look for 2008 settings.
  • +1 for Microsoft.Cpp.Win32.user.props update, needed to add prepocessor directive without changing .vcxproj
  • The Container CMakeLists.txt solution seems not working with projects using CMAKE_SOURCE_DIR.