

The SYSTEM turns the include directory into a system include directory.Ĭompilers will not issue warnings from header files originating from there. The trick is to use target_include_directories(my_library SYSTEM PUBLIC include/). There is not much I can do besides pull-requests to fix those warnings or locally disabling them,īut as a library writer you can prevent the issue for projects with you as a dependency. So the header files have a couple of instances where the warning is issued, which is annoying. So with that you have warnings automatically enabled when compiling your library, and will hopefully fix all of them.īut what if you are used by another project that has more warnings?įor example, I compile with -Wconversion but my dependencies don’t.
#Cmake flags generator
Tip: Use generator expressions to conditionally enable different warnings for different compilers.
#Cmake flags code
This code will enable -Wall for GCC and clang and /W4 for MSVC. Target_compile_options ( my_library PRIVATE $, $, $ >: -Wall> $ : /W4> ) The above warnings will work on GCC and clang, but not MSVC.īefore you start doing if(), take a look at generator expressions:

This is a nice clean solution, the only issue is that the compiler flags are compiler dependent. Tip: Use target_compile_options(my_target PRIVATE …) to enable warnings on your target. On the contrast, targets linking to it will get the include directories and other libraries as they are PUBLIC. Targets linking to it will not get the warnings enabled. You can use that to specify warnings as well.Īnd as the warnings are specified as PRIVATE, they will only be used when compiling your library. With target_compile_options() you can also specify compiler flags for your target. When creating a library you specify include directories and link to other libraries. If -DCMAKE_CXX_FLAGS="…" is annoying, why not move it into the CMakeLists.txt?Īdd_library ( my_library … ) target_include_directories ( my_library PUBLIC include/ ) target_link_libraries ( my_library PUBLIC other_library ) target_compile_options ( my_library PRIVATE -Werror -Wall -Wextra )

Enabling Warnings by Modifying Target Properties So with my latest project, foonathan/lex, I looked for a better solution. This should also be reflected by the build files. I think this is problematic, because your code is designed with a certain warning level in mind. It decouples the warning options from your version control system and build files. You either have to remove -Werror or manually disable warnings on the external target somehow. This is problematic when you use add_subdirectory() to compile some external dependencies which do not compile without warnings. The warnings are used to compile everything with warnings enabled. There compilation failed due to warnings, which was annoying. I occasionally forgot to do that, implemented a feature, pushed it to CI.

#Cmake flags update
You have to remember to manually update CMAKE_CXX_FLAGS on CI and on every locale development machine. While this approach definitely works, it has a couple of problems: That way the compiler will always have the warning flags enabled. Cmake -DCMAKE_CXX_FLAGS = "-Werror -Wall -Wextra …"
