11

I have a project like this:

|--CMakeLists.txt(1)
|--File1.cpp -W -W-all
|--Folder1
    |--CMakeLists.txt(2)
    |--File2.cpp -W -W-all -fno-rtti

As you can see above, File2.cpp needs to compile with -fno-rtti whereas the other files should compile with rtti. This is happening because I'm using both clang and boost libraries in my project. I wrote CMakeLists.txt(1) like this:

SET (CMAKE_CXX_FLAGS "-std=c++11 -fexceptions -fno-rtti -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -Wno-long-long" )

And, in CMakeLists.txt(2) I added the following:

add_definitions( -fno-rtti ) 

The above did not work. In fact none of the following have worked for me in CMakeLists.txt(2)

set_property(SOURCE File2.cpp APPEND_STRING PROPERTY CMAKE_CXX_FLAGS " -fno-rtti ")
set_property(SOURCE File2.cpp APPEND_STRING PROPERTY COMPILE_FLAGS " -fno-rtti ")
ADD_DEFINITIONS(CMAKE_CXX_FLAGS " -fno-rtti ")
ADD_DEFINITIONS(COMPILE_FLAGS " -fno-rtti ")
ADD_DEFINITIONS( -fno-rtti )

Am I missing anything?

Utkarsh Kumar
  • 567
  • 1
  • 5
  • 17
  • 1
    How do you know it didn't work? What errors do you get? Have you tried [this](https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands)? – Florian Jun 19 '17 at 18:27
  • Duplicate of this [question](https://stackoverflow.com/questions/13638408/override-compile-flags-for-single-files)? – John McFarlane Sep 29 '20 at 11:23

1 Answers1

11

AFAIK CMake does not allow to specify compile options per file. The best you can do is to create a target for the files you want to compile with -fno-rtti, e.g. with add_library, and add the compile options to this target.

add_definitions is meant to add preprocessor definitions. You should consider add_compile_options instead:

add_compile_options(-fno-rtti)

To avoid global pollution and make sure you add your options only to a given target, you may consider using target_compile_options:

target_compile_options(foo PUBLIC -fno-rtti)

Unrelated, but instead of manually setting flags to enable c++11 with set(CMAKE_CXX_FLAGS "-std=c++11 ..."), let cmake do a more fine grained job with compile features, or at least with standard selection:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

EDIT:

Looking more carefully in the documentation, you can set compile flags per source file using set_source_files_properties with COMPILE_FLAGS:

set_source_files_properties(File2.cpp PROPERTIES COMPILE_FLAGS -fno-rtti)
rocambille
  • 15,398
  • 12
  • 50
  • 68
  • 2
    To avoid global pollution wouldn't it be better to use `PRIVATE` instead of `PUBLIC` target compile options? – Florian Jun 21 '17 at 19:08
  • I'm accepting this answer, but the fix was actually in the CmakeLists.txt(2). I was not building (by calling add_library) for that individual file. Once I added the add_library, and linked it at top level , it was OK. Not sure if there is a way to workaround (must be) without creating a lib out of this one file. For ex. in XCODE we can specify directly which files need to compile with which options and generates a single executable. Does Cmake allow that ? If yes, what should I add in CMakeLists.txt(2) ? – Utkarsh Kumar Jun 23 '17 at 03:42
  • @UtkarshKumar I reread your question and the documention, `set_source_files_properties` should work for you. In your question, you used it on `File1.cpp` instead of `File2.cpp`: is that a typo? – rocambille Jun 23 '17 at 10:19
  • Thanks for correcting. I haven't tried set_source_files_properties, I think I only tried set_property. Would verify against this command and update. – Utkarsh Kumar Jun 23 '17 at 20:21
  • 1
    It looks like [`COMPILE_FLAGS`](https://cmake.org/cmake/help/v3.17/prop_sf/COMPILE_FLAGS.html#prop_sf:COMPILE_FLAGS) is now deprecated, and `COMPILE_OPTIONS` should be used instead. – Kevin Jul 08 '20 at 12:42