0

I would like that my top cmakelist creates 2 targets for 2 hardware boards.

add_executable(myExec1 ) #board_V1
add_executable(myExec2 ) #board_V2

Now I would like to pass a parameter or an option to the feature/cmakelist so that this one depending on the board choses some parameters to build (e.g. some target_definitions depending on board or some sub-feature build depending on board).

│   CMakeList.txt
│   main.c
│
└───feature
        CMakeList.txt
        feature.c
        feature.h

The project architecture is something like that and the key point for me here is that same feature target has different sub-feature depending on the calling target and to have all targets listed in one cmake.

main1   main2    main3
\_________|________/
          |
       feature
       ___|___
      /       \
    sub1     sub2   

Is this even possible ? If not, what is the cmake way to do this ?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Julien
  • 1,810
  • 1
  • 16
  • 34
  • Single `add_executable` call creates **single executable file**. That is, given file may not have different compile options at the same time. For make different executables, each of which has its own compile options, use **several** `add_executable` calls (with different target names). Note, that you may create a function or a macro, which calls `add_executable` several times, each one with the same source files but with different target name, and assign different compiler options for different targets. So, a single call to the function will create an executable for every configuration. – Tsyvarev Jul 26 '19 at 14:57
  • I am not sure to understand. I want to have 2 add_executable not a single one. My problem is I want feature to choose the files to compile instead the top cmakelist. – Julien Jul 26 '19 at 15:03
  • If you ask about how to pass something to CMake which can be checked in the `CMakeLists.txt`, then you may use `option` command. See [that question](https://stackoverflow.com/questions/5998186/adding-command-line-options-to-cmake) for more details. – Tsyvarev Jul 26 '19 at 15:57
  • If I understand correctly, you want to have the cpp files of the features only on some executable? – Guillaume Racicot Jul 26 '19 at 16:14
  • yes I want to have the cpp files (or some library) of the feature only for some executable of the same cmakelist without rerun `cmake -G` ant not the top file directly choosing the sublib. For the moment all the solutions I have thought of need `cmake -G` when the options change – Julien Jul 27 '19 at 06:54
  • You can use ``-D`` on command line or ``option`` command. Then you can have a few variables that contains a list of all the files that enables that feature. Such as ``featureA_sub1`` and ``featureA_sub2``. Then just add these files to the correct target as necessary ``add_executable(myExec1 ${featureA_sub1})`` and ``add_executable(myExec2 ${featureA_sub2})``. What is confusing about your comments is that you hint that you want to build one target with the option enabled and one target with it disabled. Then you need to make separate targets for that. – fdk1342 Jul 27 '19 at 17:10
  • @fred Is it possible that the files from feature_sub are added to target on feature level depending on myExecN options? So that main only as to know the options but not the files to add – Julien Jul 27 '19 at 18:47
  • 1
    The files have to be added either by A) `add_executable(myExec1...)` or B) `add_library(feature_sub1 ...)` and `target_link_libraries(myExec1 feature_sub1)`. So main will always have to know how to include the feature into the executable. I prefer option A. You should update this question with actual CMakeLists.txt and example file names and options if you want a concrete answer. – fdk1342 Jul 28 '19 at 14:26

0 Answers0