I would like to include a .cpp-file in two different targets (becoming two VS projects after running CMake). I would like to set different COMPILE_FLAGS for these projects.
However, when I do
SET_TARGET_PROPERTIES(myfile.cpp PROPERTIES COMPILE_FLAGS "flags1")
ADD_EXECUTABLE(project1 myfile.cpp)
SET_TARGET_PROPERTIES(myfile.cpp PROPERTIES COMPILE_FLAGS "flags2")
ADD_EXECUTABLE(project2 myfile.cpp)
the "flags2" applies for both projects, so it seems like the properties are overwritten in line 3 and not considered in line 2. Is this true or am I missing something? Is there a way to solve this?
Thank you!
I solve this problem as follows. In CMakeLists.txt:
In some header file:
Further, include above-mentioned header in both source1.cpp and source2.cpp and compile I think, CMake has no more acceptable solution
I was having the same issue (how to specify per-target precompiled header dependencies on the same source file). Luckily, the effect of set_source_files_properties is only the current directory (CMAKE_CURRENT_SOURCE_DIR). I was able to use that to come up with the following:
This works because set_source_files_properties only has an effect on the current CMakeLists.txt file. The included file will have different values for ${VARIENT} and ${CMAKE_CURRENT_BINARY_DIR} since it is included from two different CMakeLists.txt files. You can also set various compilation flags, preprocessor definitions or include paths in each of the varient's CMakeLists.txt files prior to including the LibSources.cmake file.
Apply the
set_target_properties
command to the projects and not to the source files:The flags set on the target will apply to all sources within the target.
If you adhere to a one target per subdirectory philosophy, you could do the following using
add_definitions
to add your compile flags.add_definitions
applies to all files compiled in this subdirectory and those under it. You can apply flags to specific files using the following: