CMake command line for C++ #define

后端 未结 3 601
逝去的感伤
逝去的感伤 2021-02-11 14:52

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_p         


        
相关标签:
3条回答
  • 2021-02-11 15:45

    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>
    
    0 讨论(0)
  • 2021-02-11 15:53

    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 ..
    
    0 讨论(0)
  • 2021-02-11 15:57

    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.

    0 讨论(0)
提交回复
热议问题