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.
Container
CMakeLists.txt
solutionTricky 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
):Now use your new project (
FAKE_PROJECT
) to compile. If theReadOnlyProject
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:
I managed to do it this way now:
I was able to convince everybody to add the following lines to the common CMakeLists.txt:
(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
where this cmake definition will be mapped to a C++ precompiler definition when cmake creates the project file.
To pass a C++ or C pre-processor define without modifying any CMake source files, use the environment variables
CFLAGS
for C orCXXFLAGS
for C++ respectively, e.g.: