How to add “out-of-tree” common source file

Hi

My project is an nrf sdk connect mesh made up of three applications (Swtich Light Sensor). Each application uses the common library, what would be the best setup for this type of project in terms of Cmake architecture and maintainability?

How can I add a "out of tree" common library those applications using Cmake?

Mesh
|
|
|--Light
| | |-- include
| | | | adc.h
| | |
| | |-- src
| | | | adc.c
| | |
| | | CMakeLists.txt
|
|--Switch
| | |-- include
| | | | adc.h
| | |
| | |-- src
| | | | adc.c
| | |
| | | CMakeLists.txt

|
|--Sensor
| | |-- include
| | | | adc.h
| | |
| | |-- src
| | | | adc.c
| | |
| | | CMakeLists.txt
|
|--Common
| | |-- include
| | | | adc.h
| | |
| | |-- src
| | | | adc.c
| | |

Thanks

Cedric

Parents
  • Hi Cedric,

    You can have the files in a common directory and add them to each project's CMakeLists.txt. Add the source files with target_sources() and the directory (or directories) of the header files' location(s) with target_include_directories(). If you have a few files, you can add the paths directly in target_sources() and target_include_directories(), but if you have several files, you can set a COMMON_ROOT variable.

    For example, if this is your directory structure:

    Mesh
      |___ Light
              |___ src
                      |___ main.c
              |___ prj.conf
              |___ CMakeLists.txt
      |___ Switch
              |___ src
                      |___ main.c
              |___ prj.conf
              |___ CMakeLists.txt
      |___ Sensor
              |___ src
                      |___ main.c
              |___ prj.conf
              |___ CMakeLists.txt
      |___ Common
              |___ src
                      |___ adc.c
              |___ include
                      |___ adc.h

    Then you can do the following in CMakeLists.txt:

    set(COMMON_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../common)
    
    target_sources(app PRIVATE
        src/main.c
        ${COMMON_ROOT}/include/adc.c
    )
    
    target_include_directories(app PRIVATE
        ${COMMON_ROOT}/include
    )
    

    With this, you can include adc.h in any of the projects.

    Best regards,
    Marte

Reply
  • Hi Cedric,

    You can have the files in a common directory and add them to each project's CMakeLists.txt. Add the source files with target_sources() and the directory (or directories) of the header files' location(s) with target_include_directories(). If you have a few files, you can add the paths directly in target_sources() and target_include_directories(), but if you have several files, you can set a COMMON_ROOT variable.

    For example, if this is your directory structure:

    Mesh
      |___ Light
              |___ src
                      |___ main.c
              |___ prj.conf
              |___ CMakeLists.txt
      |___ Switch
              |___ src
                      |___ main.c
              |___ prj.conf
              |___ CMakeLists.txt
      |___ Sensor
              |___ src
                      |___ main.c
              |___ prj.conf
              |___ CMakeLists.txt
      |___ Common
              |___ src
                      |___ adc.c
              |___ include
                      |___ adc.h

    Then you can do the following in CMakeLists.txt:

    set(COMMON_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../common)
    
    target_sources(app PRIVATE
        src/main.c
        ${COMMON_ROOT}/include/adc.c
    )
    
    target_include_directories(app PRIVATE
        ${COMMON_ROOT}/include
    )
    

    With this, you can include adc.h in any of the projects.

    Best regards,
    Marte

Children
Related