I’m developing a library called sonovolt, which I want to have different modules that depend on different platforms (boards, like the raspberry pi pico). Here’s the source code. My folder structure looks like this:
/ sonovolt
- /src
-- time.cpp
-- CMakeLists.txt
-- /pico
--- /include/sonovolt/pico.h
--- /pico.cpp
--- /CMakeLists.txt
- /include
-- /sonovolt
--- /time.h
--- /sonovolt.h
--- /pico.h
- CMakeLists.txt
In the end the result I want is that I can import this library like such, in case it’s the pico:
target_link_libraries(${PROJECT_NAME} pico_stdlib sonovolt sonovolt_pico)
or if the target isn’t the pico (f.ex)
target_link_libraries(${PROJECT_NAME} sonovolt)
In code it should work as:
#include "sonovolt/pico.h"
My problem is that I don’t know how to link platform specific dependencies. The following is the CMakeLists for the pico, which doesn’t build because it can’t find the pico/include/sonovolt/pico.h
header or the pico platform specific dependencies. I don’t know how this module should be defined (as INTERFACE) or otherwise.
cmake_minimum_required(VERSION 3.26)
if (NOT TARGET sonovolt_pico)
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
set(MAKE_EXPORT_COMPILE_COMMANDS ON)
# project(sonovolt_pico C CXX ASM)
pico_sdk_init()
add_library(sonovolt_pico INTERFACE)
target_include_directories(sonovolt_pico INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/*)
target_sources(sonovolt_pico
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/pico.cpp
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_link_libraries(sonovolt_pico INTERFACE sonovolt hardware_pwm)
endif()