Question about simple multi target project setup and code sharing

Hi
I’m quite new to setting up my own CMake projects and I have an issue I have not been able to solve yet despite searching around some. Maybe you can help me or point me in the right direction?

I have a C project with some shared code/libraries and two targets. However, the libraries #include configuration files that are expected to be provided by the application (#include “conf_libx.h”).

The structure is like this:

project/
  CMakeLists.txt
  common/
    CMakeLists.txt
    libx/
      libcode1.c // Includes "conf_libx.h"
      ...
  target1/
    CMakeLists.txt
    main.c
    conf_libx.h
  target2/
    CMakeLists.txt
    main.c
    conf_libx.h

How can I get both target1 and target2 to reuse the library code but provide a unique configuration for the library for each target?
“libx” is an external library that I would ideally not want to edit. I make the CMakeLists.txt file in the “common/” directory (and all other files).
If I configure libx to be a static library can I have different configurations for the different targets?
How can I expose the conf_libx.h for the library?

Very thankful for your help!

Best regards
Mattias

If you have a different configuration header, this will result in different object files. And so, different static libraries.

Some solutions:

  • do not build both targets at the same time, only one
  • combine the config headers to a single header (if possible)
  • rewrite the library to support the different features sets at run time, instead of compile time
  • Add the common library per target, with different names