Is there a better way to open libraries for links?
No, that seems beautiful.
However, you may need to reconsider the granularity with which you create static libraries. For example, if all applications except tests use only Module1 and Module2 in combination, you can combine them into one library target. Of course, the tests will contact parts of the component that they donβt use, but this is a small price to pay to reduce assembly complexity.
I am not compiling the "main" yet, what would be the correct configuration for this?
There is nothing wrong with adding it to src/CMakeLists.txt :
add_executable(my_main main.c) target_link_libraries(my_main Module1.o Module2.o)
is add_definitions the correct way to add flags to the compiler?
It may be used for this purpose, but may not be ideal.
Newer CMake scripts should use target_compile_options for this purpose. The only drawback here is that if you want to reuse the same compilation options for all the goals in your projects, you also need to make the same target_compile_options call for each of them. See below for tips on how to resolve this.
How can I make this structure DRY?
First of all, unlike most program codes, redundancy is often not a big problem in the system build code. A wonderful thing to pay attention to is things that interfere with maintainability. Returning to the general compiler options: before you want to change these flags in the future, it is likely that you want to change them for each purpose. It makes sense to centralize knowledge of options: either enter function at the top level, which sets an option for a given target, or save the parameters of a global variable.
In any case, you will need to write one line for each purpose in order to get this option, but after that it will not generate unnecessary maintenance costs. As an added bonus, if you really need to change the option for just one purpose in the future, you still have the opportunity to do so.
However, be careful not to overload things. The build system should do everything first.
If the easiest way is to set it up, it means you copy / paste a lot, follow it! If during servicing later it turns out that you have real unnecessary redundancies, you can always reorganize.
The sooner you accept the fact that your CMake scripts will never be as beautiful as your program code, the better;)
One little nitpick at the end: Avoid providing extensions to your target names. That is, instead of
add_library(Libtap.o tap.c)
consider
add_library(Libtap tap.c)
CMake will automatically add the correct file ending depending on the target platform.