Build error as header file not found

Hi all

I hve a C project for raspberry pico and I’m trying to organise it properly so it looks legible and decent. I am using the tinyusb library and one of the main ones that is important is tusb_config.h, and after reformatting and cleaning up my code enough I get this error

path/src/tusb_option.h:257:12: fatal error: tusb_config.h: No such file or directory
257 | #include “tusb_config.h”
| ^~~~~~~~~~~~~~~

  1. Below is the app main source file which uses tusb_config.h and I have included the error using file sets, as recommended in ProfessionalCMake. However, it still isn’t working
add_executable(app_main main.c)

target_sources(app_main
   PUBLIC
    FILE_SET HEADERS
    BASE_DIRS
        ${CMAKE_SOURCE_DIR}/src
        ${CMAKE_SOURCE_DIR}/file_processing
        ${CMAKE_CURRENT_SOURCE_DIR})


## target_include_directories(app_main PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PUBLIC ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/file_processing)
target_link_libraries(app_main PUBLIC file_processing host_storage fatfs_csv)

pico_add_extra_outputs(app_main)

  1. Below is the subdirectory which includes the tusb_config.h. Also as a side question, do I have to include header files into the add_library command, or can it be only .c files.
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)




set(FATFS_LIB_HEADERS

    $ENV{PICO_TINYUSB_PATH}/lib/fatfs/source

)




set(SRC_SOURCES

    msc_app.c

    )




set(SRC_HEADERS

    msc_app.h

    tusb_config.h

    embedded_cli.h)





add_library(host_storage STATIC

         ${SRC_SOURCES} ${SRC_HEADERS} ${FATFS_LIB_SOURCES} ${FATFS_LIB_HEADERS}  

        )




target_sources(host_storage 

    PUBLIC

        FILE_SET HEADERS 

        BASE_DIRS

            ${FATFS_LIB_HEADERS}

            ${CMAKE_CURRENT_SOURCE_DIR}




)        




# target_include_directories(host_storage

#     PUBLIC

#        ${CMAKE_CURRENT_SOURCE_DIR}

#        ${FATFS_LIB_HEADERS}

# )




target_link_libraries(host_storage

    PUBLIC

        tinyusb_host

        tinyusb_board

        pico_stdlib

)

Any help or advice would be much appreciated

Kind regards

That would explain the error with the #include. You need to specify the header files:

target_sources(app_main
   PUBLIC
    FILE_SET HEADERS
    BASE_DIRS
        ${CMAKE_SOURCE_DIR}/src
        ${CMAKE_SOURCE_DIR}/file_processing
        ${CMAKE_CURRENT_SOURCE_DIR}
    # Or just manually list the headers here directly instead of
    # expanding "${SRC_HEADERS}"
    FILES ${SRC_HEADERS})

`target_sources` will complain if for any reason the headers in question aren’t within one within one of the BASE_DIRS you’ve specified. That is intentional and desirable; either move your headers within one of the BASE_DIRS or change your BASE_DIRS.

Only the .c/.cpp files are usually needed. For the headers, all you need is to make sure the headers are available when building the library and when some other target (e.g. app_main) uses that library. As long as you’re using target_sources(your_library ...) and target_link_libraries(app_main PUBLIC/PRIVATE/INTERFACE <libraries...>) everywhere, you should be okay.