How to define a C++ preprocessor macro through the command line with CMake?

后端 未结 4 620
野性不改
野性不改 2020-12-01 05:25

I try to set a preprocessor macro in the command line of CMake. I\'ve tried:

set generator=\"Visual Studio 8 2005\"
set params=-D MY_MACRO=1
cmake.exe -G %ge         


        
4条回答
  •  萌比男神i
    2020-12-01 06:02

    The motivation behind the question was to batch build 3rd party libraries, which is why I wanted to avoid modifying CMakeLists. So years later, even though I don't need that anymore, I figured out that it's easily achievable by means external to CMake:

    • Invoke CMake as usual, no special flags.

    • Then:

      • With MSVC: The compiler reads the CL environment variable to get extra command line arguments. So

          set CL=/DMY_MACRO=1 %CL%
        

        then invoke MSBuild to do its job.

      • With Makefiles: The generated makefiles use the CFLAGS and CXX_FLAGS variables as makefiles are expected to do. So the build can be started by

          make CXX_FLAGS=-DMY_MACRO=1
        

        or by setting the corresponding environment variables.

提交回复
热议问题