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

6 Replies 6

Which source file is compiled when you got that error about header? Also, what exact location of the header tusb_config.h? Is its path <project_dir>/src/tusb_config.h or someone else?

This is the error I think you're talking about:

In file included from H:/tinyusb/src/host/hub.c:27:
compilation terminated.

Also it is in the src directory, like you mentioned.

According to path H:/tinyusb/src/host/hub.c, that file should be part of src_lib library, but it is not listed in your src/CMakeLists.txt. Filesets affect only on compilation of source files listed for a specific targets. Without knowing which CMake target the file H:/tinyusb/src/host/hub.c belongs to, it is impossible to solve the error you got.

How do I find out what target it belongs to? I'm a newbie in CMakeLists

You define a target with add_executable or add_library call. Source files could be specified directly in that call, or could be added later via target_sources calls.

There is no target it belongs to, its just a bunch of source and header files.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.