# How to change build flag for just a few source files in a project for debugging

**URL:** <https://discourse.cmake.org/t/how-to-change-build-flag-for-just-a-few-source-files-in-a-project-for-debugging/1585>\
**Category:** Code\
**Created:** [July 18, 2020, 2:14pm UTC](https://discourse.cmake.org/t/how-to-change-build-flag-for-just-a-few-source-files-in-a-project-for-debugging/1585 "2020-07-18T14:14:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![butette](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@butette](https://discourse.cmake.org/u/butette)\
**Post date:** [July 18, 2020, 2:14pm UTC](https://discourse.cmake.org/t/how-to-change-build-flag-for-just-a-few-source-files-in-a-project-for-debugging/1585/1 "2020-07-18T14:14:07Z")

</div>

If I already have a project set up with a standard set of compile/link options for that build type and need to debug by changing the compile and or link options for just one or two source files and leave the existing compile/link options for the rest of the project without having to rebuild the entire project with the modified compile/link options, is there a recommended way to do this? In standard Make I would just create a special rule for those source files that I want to impact and rebuild.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [July 18, 2020, 2:22pm UTC](https://discourse.cmake.org/t/how-to-change-build-flag-for-just-a-few-source-files-in-a-project-for-debugging/1585/2 "2020-07-18T14:22:55Z")

</div>

Have a look at source file property [COMPILE\_OPTIONS](https://cmake.org/cmake/help/latest/prop_sf/COMPILE_OPTIONS.html).

For link options, there is no source file property for that because this step is related to the target is so is the same for **all** sources. See [LINK\_OPTIONS](https://cmake.org/cmake/help/latest/prop_tgt/LINK_OPTIONS.html).

But keep in mind that changing a `CMakeLists.txt` will trigger a new generation step before compilation step.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [July 18, 2020, 11:15pm UTC](https://discourse.cmake.org/t/how-to-change-build-flag-for-just-a-few-source-files-in-a-project-for-debugging/1585/3 "2020-07-18T23:15:26Z")

</div>

Due to a limitation with the way CMake’s Makefiles generator works, if you change a source file property for one source in a target, all sources for that target will be recompiled. This limitation does not apply to other generators, so switching to Ninja may be a better choice for this workflow.

---

<div class="post-metadata">

**Author:** ![butette](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@butette](https://discourse.cmake.org/u/butette)\
**Post date:** [July 19, 2020, 3:37pm UTC](https://discourse.cmake.org/t/how-to-change-build-flag-for-just-a-few-source-files-in-a-project-for-debugging/1585/4 "2020-07-19T15:37:11Z")

</div>

Thanks Craig and Marc. I will try Ninja and see but in my current build using plain old GNU Makefile, I do what I mentioned above all the time w/o triggering any massive rebuild. If I have a library already build with say -O3 optimization level and want to debug a specific module with -O0, all I doin my current Makefile is to define a new rule for that module and only that one got recompile and replaced the previous version in the library, no massive recompilation for the rest of the unaffected codes.
