How to use file sets in CMakeLists
04:47 19 Nov 2025

I'm currently writing CMakeLists for a multidirectory project,and am trying to use file sets and target_sources as it is regarded as better than using target_include_directories, but the only problem being that I'm not fully sure I'm using it correctly, and would like help with it

cmake_minimum_required(VERSION 3.27)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(pico_sdk_import.cmake)

project(msc_bare_metal C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()


add_subdirectory(file_processing)
add_subdirectory(src)
add_subdirectory(app)

This is the CMakeLists.txt for file_processing

set(FATFS_LIB_SOURCES 
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c 
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/ffsystem.c 
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/ffunicode.c
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/diskio.c
)

set(FATFS_LIB_HEADERS
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/diskio.h
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/ffconf.h
    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source/ff.h
)

add_library(file_processing)

# Add C sources normally
target_sources(file_processing
    PRIVATE
        ${FATFS_LIB_SOURCES}
        file_processing.c
)


target_sources(file_processing
    PRIVATE
        FILE_SET HEADERS
        BASE_DIRS
            $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source
            ${CMAKE_CURRENT_LIST_DIR}
        FILES
            ${FATFS_LIB_HEADERS}
            file_processing.h
)



# No need for target_include_directories(), file set handles it

target_link_libraries(file_processing
    PUBLIC
        pico_stdlib
        tinyusb_host
        tinyusb_board
        hardware_i2c
)

and this is the CMakeLists.txt for the src

set(LIBRARY_HEADER  tusb_config.h  msc_app.h )

add_library(src_lib)

target_sources(src_lib
        PRIVATE
           msc_app.c    )

target_sources(src_lib
    PUBLIC
        FILE_SET HEADERS
        BASE_DIRS
           ${CMAKE_CURRENT_LIST_DIR}
        FILES
            ${LIBRARY_HEADER} )

 target_link_libraries(src_lib PRIVATE file_processing)

and for the one in the app subdirectory

add_executable(main main.c)
target_link_libraries(main PRIVATE file_processing src_lib)
pico_add_extra_outputs(main)

The error im getting is that

H:/tinyusb/src/tusb_option.h:257:12: fatal error: tusb_config.h: No such file or directory 257 | #include "tusb_config.h" | ^~~~~~~~~~~~~~~

Which I don't understand since I included it in the target_sources using the variable ${LIBRARY_HEADER} and would like to know what I'm doing wrong.

Kind regards

c++ c cmake raspberry-pi-pico