Does set_target_properties in CMake override CMAKE_CXX_FLAGS?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







52















At the beginning of my CMake project, I'm setting general compilation flags in the variable CMAKE_CXX_FLAGS, like



set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")


Later on, I need to append additional configuration-specific compilation flags (stored in BUILD_FLAGS). Can I use the following command for this:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


or do I have to add the CMAKE_CXX_FLAGS manually:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")


to prevent CMAKE_CXX_FLAGS being overriden by BUILD_FLAGS?










share|improve this question


















  • 2





    I believe that they are appended to the CMAKE_CXX_FLAGS, you can verify this by invoking a verbose make file make target VERBOSE=1

    – Ramon Zarazua B.
    Feb 23 '11 at 21:49


















52















At the beginning of my CMake project, I'm setting general compilation flags in the variable CMAKE_CXX_FLAGS, like



set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")


Later on, I need to append additional configuration-specific compilation flags (stored in BUILD_FLAGS). Can I use the following command for this:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


or do I have to add the CMAKE_CXX_FLAGS manually:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")


to prevent CMAKE_CXX_FLAGS being overriden by BUILD_FLAGS?










share|improve this question


















  • 2





    I believe that they are appended to the CMAKE_CXX_FLAGS, you can verify this by invoking a verbose make file make target VERBOSE=1

    – Ramon Zarazua B.
    Feb 23 '11 at 21:49














52












52








52


13






At the beginning of my CMake project, I'm setting general compilation flags in the variable CMAKE_CXX_FLAGS, like



set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")


Later on, I need to append additional configuration-specific compilation flags (stored in BUILD_FLAGS). Can I use the following command for this:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


or do I have to add the CMAKE_CXX_FLAGS manually:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")


to prevent CMAKE_CXX_FLAGS being overriden by BUILD_FLAGS?










share|improve this question














At the beginning of my CMake project, I'm setting general compilation flags in the variable CMAKE_CXX_FLAGS, like



set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")


Later on, I need to append additional configuration-specific compilation flags (stored in BUILD_FLAGS). Can I use the following command for this:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


or do I have to add the CMAKE_CXX_FLAGS manually:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")


to prevent CMAKE_CXX_FLAGS being overriden by BUILD_FLAGS?







c++ cmake compiler-flags






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 23 '11 at 20:46









Milan HanusMilan Hanus

275134




275134








  • 2





    I believe that they are appended to the CMAKE_CXX_FLAGS, you can verify this by invoking a verbose make file make target VERBOSE=1

    – Ramon Zarazua B.
    Feb 23 '11 at 21:49














  • 2





    I believe that they are appended to the CMAKE_CXX_FLAGS, you can verify this by invoking a verbose make file make target VERBOSE=1

    – Ramon Zarazua B.
    Feb 23 '11 at 21:49








2




2





I believe that they are appended to the CMAKE_CXX_FLAGS, you can verify this by invoking a verbose make file make target VERBOSE=1

– Ramon Zarazua B.
Feb 23 '11 at 21:49





I believe that they are appended to the CMAKE_CXX_FLAGS, you can verify this by invoking a verbose make file make target VERBOSE=1

– Ramon Zarazua B.
Feb 23 '11 at 21:49












2 Answers
2






active

oldest

votes


















50














Use the first one:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.




COMPILE_FLAGS



   Additional flags to use when compiling this target's sources. 

The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to
pass additional preprocessor definitions.



The full command line will be the equivalent of:



${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc


And as Ramon said, you can always check with make VERBOSE=1.






share|improve this answer





















  • 1





    You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

    – Milan Hanus
    Feb 24 '11 at 16:34



















49














The accepted answer is still working but outdated since 2013.

This answer is based and new functions from CMake v2.8.12, v3.3 and v3.13.



Since CMake-2.8.12 (2013)



Two new commands to set CMAKE_CXX_FLAGS:





  • target_compile_options() (for one single target)


  • add_compile_options() (for all targets)


The documentation of last version has not changed a lot since cmake-2.8.12:





  • target_compile_options()

  • add_compile_options()


In you case you can use:



target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})


Or simply if you have a single target:



add_compile_options(${BUILD_FLAGS})


More examples



target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC -g) # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options( mylib PUBLIC -DUSEXX) # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX) # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX) # Bad
add_definitions(-DUSEXX) # OK


Deprecated COMPILE_FLAGS



cmake-3.0 documentation flags COMPILE_FLAGS as deprecated:




COMPILE_FLAGS



Additional flags to use when compiling this target’s sources.



The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to pass
additional preprocessor definitions.



This property is deprecated. Use the COMPILE_OPTIONS property or the
target_compile_options command instead.




If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:



set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})


Since CMake-3.3 (2015)



Anton Petrov suggests to use generator expressions as presented in an answer of ar31.



The CMake generator expressions applies your ${BUILD_FLAGS} to:




  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)

  • Clang compiler using $<CXX_COMPILER_ID:Clang>

    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)

    (use $<C_COMPILER_ID:Clang> instead if language is C)

  • and more as supported C++ feature or compiler version... (see documentation)


In you case you can use:



target_compile_options(${TARGET} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)


or about compilers:



target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
$<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
$<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)


Since CMake-3.13 (2018)



A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.



Different options for C and C++ files



The best way is to distinguish C files and C++ files using two different targets.






share|improve this answer


























  • What if you have mixed target? For example C and C++. How do you set options only for C target?

    – Martin
    Jun 2 '17 at 22:37











  • Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

    – olibre
    Jun 3 '17 at 7:26











  • Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

    – Royi
    Feb 21 '18 at 0:42






  • 1





    @olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

    – Anton Petrov
    Mar 2 '18 at 8:04






  • 1





    $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

    – Adam Romanek
    Jan 7 at 11:48












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5096881%2fdoes-set-target-properties-in-cmake-override-cmake-cxx-flags%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









50














Use the first one:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.




COMPILE_FLAGS



   Additional flags to use when compiling this target's sources. 

The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to
pass additional preprocessor definitions.



The full command line will be the equivalent of:



${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc


And as Ramon said, you can always check with make VERBOSE=1.






share|improve this answer





















  • 1





    You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

    – Milan Hanus
    Feb 24 '11 at 16:34
















50














Use the first one:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.




COMPILE_FLAGS



   Additional flags to use when compiling this target's sources. 

The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to
pass additional preprocessor definitions.



The full command line will be the equivalent of:



${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc


And as Ramon said, you can always check with make VERBOSE=1.






share|improve this answer





















  • 1





    You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

    – Milan Hanus
    Feb 24 '11 at 16:34














50












50








50







Use the first one:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.




COMPILE_FLAGS



   Additional flags to use when compiling this target's sources. 

The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to
pass additional preprocessor definitions.



The full command line will be the equivalent of:



${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc


And as Ramon said, you can always check with make VERBOSE=1.






share|improve this answer















Use the first one:



set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})


The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.




COMPILE_FLAGS



   Additional flags to use when compiling this target's sources. 

The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to
pass additional preprocessor definitions.



The full command line will be the equivalent of:



${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc


And as Ramon said, you can always check with make VERBOSE=1.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 24 '11 at 7:27

























answered Feb 24 '11 at 7:18









richqrichq

47.2k18140140




47.2k18140140








  • 1





    You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

    – Milan Hanus
    Feb 24 '11 at 16:34














  • 1





    You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

    – Milan Hanus
    Feb 24 '11 at 16:34








1




1





You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

– Milan Hanus
Feb 24 '11 at 16:34





You are right, thank you very much (and to Ramon too). I also didn't know about the VERBOSE parameter before and it is now much easier for me to learn how CMake actually processes my commands.

– Milan Hanus
Feb 24 '11 at 16:34













49














The accepted answer is still working but outdated since 2013.

This answer is based and new functions from CMake v2.8.12, v3.3 and v3.13.



Since CMake-2.8.12 (2013)



Two new commands to set CMAKE_CXX_FLAGS:





  • target_compile_options() (for one single target)


  • add_compile_options() (for all targets)


The documentation of last version has not changed a lot since cmake-2.8.12:





  • target_compile_options()

  • add_compile_options()


In you case you can use:



target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})


Or simply if you have a single target:



add_compile_options(${BUILD_FLAGS})


More examples



target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC -g) # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options( mylib PUBLIC -DUSEXX) # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX) # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX) # Bad
add_definitions(-DUSEXX) # OK


Deprecated COMPILE_FLAGS



cmake-3.0 documentation flags COMPILE_FLAGS as deprecated:




COMPILE_FLAGS



Additional flags to use when compiling this target’s sources.



The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to pass
additional preprocessor definitions.



This property is deprecated. Use the COMPILE_OPTIONS property or the
target_compile_options command instead.




If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:



set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})


Since CMake-3.3 (2015)



Anton Petrov suggests to use generator expressions as presented in an answer of ar31.



The CMake generator expressions applies your ${BUILD_FLAGS} to:




  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)

  • Clang compiler using $<CXX_COMPILER_ID:Clang>

    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)

    (use $<C_COMPILER_ID:Clang> instead if language is C)

  • and more as supported C++ feature or compiler version... (see documentation)


In you case you can use:



target_compile_options(${TARGET} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)


or about compilers:



target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
$<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
$<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)


Since CMake-3.13 (2018)



A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.



Different options for C and C++ files



The best way is to distinguish C files and C++ files using two different targets.






share|improve this answer


























  • What if you have mixed target? For example C and C++. How do you set options only for C target?

    – Martin
    Jun 2 '17 at 22:37











  • Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

    – olibre
    Jun 3 '17 at 7:26











  • Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

    – Royi
    Feb 21 '18 at 0:42






  • 1





    @olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

    – Anton Petrov
    Mar 2 '18 at 8:04






  • 1





    $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

    – Adam Romanek
    Jan 7 at 11:48
















49














The accepted answer is still working but outdated since 2013.

This answer is based and new functions from CMake v2.8.12, v3.3 and v3.13.



Since CMake-2.8.12 (2013)



Two new commands to set CMAKE_CXX_FLAGS:





  • target_compile_options() (for one single target)


  • add_compile_options() (for all targets)


The documentation of last version has not changed a lot since cmake-2.8.12:





  • target_compile_options()

  • add_compile_options()


In you case you can use:



target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})


Or simply if you have a single target:



add_compile_options(${BUILD_FLAGS})


More examples



target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC -g) # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options( mylib PUBLIC -DUSEXX) # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX) # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX) # Bad
add_definitions(-DUSEXX) # OK


Deprecated COMPILE_FLAGS



cmake-3.0 documentation flags COMPILE_FLAGS as deprecated:




COMPILE_FLAGS



Additional flags to use when compiling this target’s sources.



The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to pass
additional preprocessor definitions.



This property is deprecated. Use the COMPILE_OPTIONS property or the
target_compile_options command instead.




If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:



set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})


Since CMake-3.3 (2015)



Anton Petrov suggests to use generator expressions as presented in an answer of ar31.



The CMake generator expressions applies your ${BUILD_FLAGS} to:




  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)

  • Clang compiler using $<CXX_COMPILER_ID:Clang>

    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)

    (use $<C_COMPILER_ID:Clang> instead if language is C)

  • and more as supported C++ feature or compiler version... (see documentation)


In you case you can use:



target_compile_options(${TARGET} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)


or about compilers:



target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
$<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
$<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)


Since CMake-3.13 (2018)



A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.



Different options for C and C++ files



The best way is to distinguish C files and C++ files using two different targets.






share|improve this answer


























  • What if you have mixed target? For example C and C++. How do you set options only for C target?

    – Martin
    Jun 2 '17 at 22:37











  • Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

    – olibre
    Jun 3 '17 at 7:26











  • Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

    – Royi
    Feb 21 '18 at 0:42






  • 1





    @olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

    – Anton Petrov
    Mar 2 '18 at 8:04






  • 1





    $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

    – Adam Romanek
    Jan 7 at 11:48














49












49








49







The accepted answer is still working but outdated since 2013.

This answer is based and new functions from CMake v2.8.12, v3.3 and v3.13.



Since CMake-2.8.12 (2013)



Two new commands to set CMAKE_CXX_FLAGS:





  • target_compile_options() (for one single target)


  • add_compile_options() (for all targets)


The documentation of last version has not changed a lot since cmake-2.8.12:





  • target_compile_options()

  • add_compile_options()


In you case you can use:



target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})


Or simply if you have a single target:



add_compile_options(${BUILD_FLAGS})


More examples



target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC -g) # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options( mylib PUBLIC -DUSEXX) # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX) # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX) # Bad
add_definitions(-DUSEXX) # OK


Deprecated COMPILE_FLAGS



cmake-3.0 documentation flags COMPILE_FLAGS as deprecated:




COMPILE_FLAGS



Additional flags to use when compiling this target’s sources.



The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to pass
additional preprocessor definitions.



This property is deprecated. Use the COMPILE_OPTIONS property or the
target_compile_options command instead.




If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:



set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})


Since CMake-3.3 (2015)



Anton Petrov suggests to use generator expressions as presented in an answer of ar31.



The CMake generator expressions applies your ${BUILD_FLAGS} to:




  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)

  • Clang compiler using $<CXX_COMPILER_ID:Clang>

    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)

    (use $<C_COMPILER_ID:Clang> instead if language is C)

  • and more as supported C++ feature or compiler version... (see documentation)


In you case you can use:



target_compile_options(${TARGET} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)


or about compilers:



target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
$<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
$<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)


Since CMake-3.13 (2018)



A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.



Different options for C and C++ files



The best way is to distinguish C files and C++ files using two different targets.






share|improve this answer















The accepted answer is still working but outdated since 2013.

This answer is based and new functions from CMake v2.8.12, v3.3 and v3.13.



Since CMake-2.8.12 (2013)



Two new commands to set CMAKE_CXX_FLAGS:





  • target_compile_options() (for one single target)


  • add_compile_options() (for all targets)


The documentation of last version has not changed a lot since cmake-2.8.12:





  • target_compile_options()

  • add_compile_options()


In you case you can use:



target_compile_options(${TARGET} PRIVATE ${BUILD_FLAGS})


Or simply if you have a single target:



add_compile_options(${BUILD_FLAGS})


More examples



target_compile_options(mylib PRIVATE   -O2) # only internal
target_compile_options(mylib INTERFACE -gl) # only external
target_compile_options(mylib PUBLIC -g) # same as PRIVATE + INTERFACE

# multiple targets and flags
target_compile_options(mylib1 mylib2 PRIVATE -Wall -Wextra)

target_compile_options( mylib PUBLIC -DUSEXX) # Bad
target_compile_definitions(mylib PUBLIC -DUSEXX) # OK

add_compile_options(-Wall -Wextra) # for all targets in current directory
add_compile_options(-DUSEXX) # Bad
add_definitions(-DUSEXX) # OK


Deprecated COMPILE_FLAGS



cmake-3.0 documentation flags COMPILE_FLAGS as deprecated:




COMPILE_FLAGS



Additional flags to use when compiling this target’s sources.



The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to pass
additional preprocessor definitions.



This property is deprecated. Use the COMPILE_OPTIONS property or the
target_compile_options command instead.




If you still want to use set_target_properties() you may use COMPILE_OPTIONS instead of COMPILE_FLAGS:



set_target_properties(${TARGET} PROPERTIES COMPILE_OPTIONS ${BUILD_FLAGS})


Since CMake-3.3 (2015)



Anton Petrov suggests to use generator expressions as presented in an answer of ar31.



The CMake generator expressions applies your ${BUILD_FLAGS} to:




  • C++ language using $<COMPILE_LANGUAGE:CXX> (can also be C, CUDA...)

  • Clang compiler using $<CXX_COMPILER_ID:Clang>

    (can also be GNU for gcc, or MSVCfor Visual C++... see full list)

    (use $<C_COMPILER_ID:Clang> instead if language is C)

  • and more as supported C++ feature or compiler version... (see documentation)


In you case you can use:



target_compile_options(${TARGET} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:${BUILD_FLAGS_FOR_CXX}>
$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>)


or about compilers:



target_compile_options(${TARGET} PRIVATE
$<$<CXX_COMPILER_ID:Clang>:${BUILD_FLAGS_FOR_CLANG}>
$<$<CXX_COMPILER_ID:GNU>:${BUILD_FLAGS_FOR_GCC}>
$<$<CXX_COMPILER_ID:MSVC>:${BUILD_FLAGS_FOR_VISUAL}>)


Since CMake-3.13 (2018)



A new function target_link_options() allow to pass options to the linker, as mentioned by Craig Scott.



Different options for C and C++ files



The best way is to distinguish C files and C++ files using two different targets.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 19 at 21:39

























answered Jun 26 '15 at 14:20









olibreolibre

27.7k16117157




27.7k16117157













  • What if you have mixed target? For example C and C++. How do you set options only for C target?

    – Martin
    Jun 2 '17 at 22:37











  • Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

    – olibre
    Jun 3 '17 at 7:26











  • Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

    – Royi
    Feb 21 '18 at 0:42






  • 1





    @olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

    – Anton Petrov
    Mar 2 '18 at 8:04






  • 1





    $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

    – Adam Romanek
    Jan 7 at 11:48



















  • What if you have mixed target? For example C and C++. How do you set options only for C target?

    – Martin
    Jun 2 '17 at 22:37











  • Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

    – olibre
    Jun 3 '17 at 7:26











  • Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

    – Royi
    Feb 21 '18 at 0:42






  • 1





    @olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

    – Anton Petrov
    Mar 2 '18 at 8:04






  • 1





    $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

    – Adam Romanek
    Jan 7 at 11:48

















What if you have mixed target? For example C and C++. How do you set options only for C target?

– Martin
Jun 2 '17 at 22:37





What if you have mixed target? For example C and C++. How do you set options only for C target?

– Martin
Jun 2 '17 at 22:37













Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

– olibre
Jun 3 '17 at 7:26





Hi @Martin. I do not know about a target mixing C and C++ files using different options each kind of source file. I may suggest to split this target in two : one target for C source code and the other one for C++. ... OK I am starting to add a section in may answer to propose a way to address this complex situation...

– olibre
Jun 3 '17 at 7:26













Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

– Royi
Feb 21 '18 at 0:42





Why can't I use set_target_properties() with LINK_FLAGS the same way? It seems it never accepts more than one flag (Even in list form).

– Royi
Feb 21 '18 at 0:42




1




1





@olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

– Anton Petrov
Mar 2 '18 at 8:04





@olibre Please see this answer stackoverflow.com/a/21561742/423959 for language specific options. The right way is to use generator expressions.

– Anton Petrov
Mar 2 '18 at 8:04




1




1





$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

– Adam Romanek
Jan 7 at 11:48





$<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_CXX}> is probably a mistake, it should rather be $<$<COMPILE_LANGUAGE:C>:${BUILD_FLAGS_FOR_C}>

– Adam Romanek
Jan 7 at 11:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5096881%2fdoes-set-target-properties-in-cmake-override-cmake-cxx-flags%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)