Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:30:35+00:00 2026-06-15T09:30:35+00:00

I would like to use a global set of flags for compiling a project,

  • 0

I would like to use a global set of flags for compiling a project, meaning that at my top-level CMakeLists.txt file I have specified:

ADD_DEFINITIONS ( -Wall -Weffc++ -pedantic -std=c++0x )

However, for a specific file (let’s say “foo.cpp”) in a subdirectory, I want to switch the
compile flags to not apply -Weffc++ (included commercial library I cannot change). To simplify the situation to use only -Wall, I tried:

 SET_SOURCE_FILES_PROPERTIES( foo.cpp PROPERTIES COMPILE_FLAGS -Wall )
 ADD_EXECUTABLE( foo foo.cpp )

, which did not work.
I also tried

SET_PROPERTY( SOURCE foo.cpp PROPERTY COMPILE_FLAGS -Wall )
ADD_EXECUTABLE( foo foo.cpp )

and

ADD_EXECUTABLE( foo foo.cpp )
SET_TARGET_PROPERTIES( foo PROPERTIES COMPILE_FLAGS -Wall )

, in which neither worked.

Finally, I tried removing this defintion:

REMOVE_DEFINITIONS( -Weffc++ )
ADD_EXECUTABLE( foo foo.cpp )
ADD_DEFINITIONS( -Weffc++ )

, which also did not work (meaning, I get a lot of style warnings about the commercial library).
(**Note: The warnings ARE suppressed if I DO NOT re-include the -Weffc++ directive after the executable is built.)

I also tried temporarily removing the compile flags:
http://www.cmake.org/pipermail/cmake/2007-June/014614.html
, but that didn’t help.

Is there not an elegant solution to this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:30:36+00:00Added an answer on June 15, 2026 at 9:30 am

    Your attempts above are adding further flags to your file/target rather than overwriting as you seem to expect. For example, from the docs for Properties on Source Files – COMPILE_FLAGS:

    These flags will be added to the list of compile flags when this source file builds.

    You should be able to countermand the -Weffc++ flag for foo.cpp by doing

    set_source_files_properties(foo.cpp PROPERTIES COMPILE_FLAGS -Wno-effc++)
    

    This should have the effect of adding -Wno-effc++ after -Weffc++ in the compiler command, and the latter setting wins. To see the full command and check that this is indeed the case, you can do

    make VERBOSE=1
    

    As an aside, one of the maintainers of the GNU C++ Standard Library presents a pretty negative opinion on -Weffc++ in this answer.

    Another point is that you’re misusing add_definitions in the sense that you’re using this for compiler flags rather than the intended preprocessor definitions.

    It would be preferable to use add_compile_options

    add_compile_options(-Wall -Weffc++ -pedantic -std=c++0x)
    

    or for CMake versions < 3.0 to do something more like:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Weffc++ -pedantic -std=c++0x")
    

    In response to further questions in the comments below, I believe it’s impossible to reliably remove a flag on a single file. The reason is that for any given source file, it has the COMPILE_OPTIONS and COMPILE_FLAGS1 of its target applied, but these don’t show up in any of the properties for that source file.

    You could look at stripping the problem flag from the target’s COMPILE_OPTIONS, then applying it to each of the target’s sources individually, omitting it from the specific source file as required.

    However, while this could work in many scenarios, it has a couple of problems.

    First – source files’ properties don’t include COMPILE_OPTIONS, only COMPILE_FLAGS. This is a problem because the COMPILE_OPTIONS of a target can include generator expressions, but COMPILE_FLAGS doesn’t support them. So you’d have to accommodate generator expressions while searching for your flag, and indeed you’d maybe even have to "parse" generator expressions if your flag was contained in one or more to see whether it should be re-applied to the remaining source files.

    Second – since CMake v3.0, targets can specify INTERFACE_COMPILE_OPTIONS. This means that a dependency of your target can add or override your target’s COMPILE_OPTIONS via its INTERFACE_COMPILE_OPTIONS. So you’d further have to recursively iterate through all your target’s dependencies (not a particularly easy task since the list of LINK_LIBRARIES for the target can also contain generator expressions) to find any which are applying the problem flag, and try and remove it from those targets’ INTERFACE_COMPILE_OPTIONS too.

    At this stage of complexity, I’d be looking to submit a patch to CMake to provide the functionality to remove a specific flag unconditionally from a source file.


    1: Note that unlike the COMPILE_FLAGS property on source files, the COMPILE_FLAGS property on targets is deprecated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use set of custom Exception classes in my project based
I would like to set up a route for a controller that has the
I would like to be able to use the global options from an argparse.ArgumentParser
I would like to use a configuration in the .config file like this: <appSettings>
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted
I would like to use Maven's password encryption such as it uses for nodes
I would like to use the logout function from Django but not sure how
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using
I would like to use the jenkins script console some more. Where do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.