Building a Library

I want to make a library for my stm32 where i can use peripherals like
#include “hardware/gpio.h”
#include “hardware/uart.h”
i have all the code but need to build that trying cmake but failing can anyone please help with the structure and cmake

What have you done so far, do you have a draft of your project file (CMakeLists.txt)?
If you know absolutely nothing about CMake, I would recommend you to go through CMake Tutorial.

I did before I can Do if every file is in a single dir for example hardware(src, inc) but for a bit more complex structure i still can do but i want to do it in such way so that i can include them as #include “hardware/gpio.h” can you please help me about how the structure should be or should I compile them as library or just use them as include directory and so on

So how are you doing it at the moment? Show us what you’ve done so far.
Also, I must admit, I barely understood anything from your post.

add_library(uart STATIC
  uart.c
  uart.h
  # Add other uart source files here
)

# Include directories for uart
target_include_directories(uart PUBLIC ${CMAKE_SOURCE_DIR}/core)
#This the cmakefile inside myproject/hardware/uart
# Add subdirectories for each peripheral
add_subdirectory(i2c)
add_subdirectory(uart)
add_subdirectory(clock)
add_subdirectory(spi)
#this is the cmakefile inside myproject/hardware
cmake_minimum_required(VERSION 3.20)

set(TARGET STM32F4XX)



include(arm-none-eabi.cmake)

project(${TARGET} VERSION 1.0.0 LANGUAGES ASM C CXX)

add_subdirectory(hardware)

add_executable(${TARGET} src/main.c)

target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core ${CMAKE_CURRENT_SOURCE_DIR}/inc)


if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Debug)
endif()


target_link_libraries(${TARGET} i2c uart)

target_link_options(${TARGET} PRIVATE
                    -T${LINKER_SCRIPT}
                    -Wl,--gc-sections
                    -Wl,--print-memory-usage
                    -Wl,-Map=${TARGET}.map
                    # -specs=nano.specs
                    -lstdc++
                    -lc 
                    -lm
                    )
#this is the top level cmake file of my project

in this way it is working but i have to use relative path
#inlclude "../hardware/uart/uart.h"
but i want like
#include "hardware/uart.h"

Okay, that makes it more clear. Right then, looking at this:

add_library(uart STATIC
  uart.c
  uart.h
)

I assume that uart.h path is myproject/hardware/uart/uart.h, but then here:

target_include_directories(uart PUBLIC ${CMAKE_SOURCE_DIR}/core)

you are adding core, whose path seems to be myproject/core, so this looks like a typo, and instead it should be this, I guess:

# you probably shouldn't assume that your project will always be the top-level one to use CMAKE_SOURCE_DIR
target_include_directories(uart PUBLIC ${CMAKE_SOURCE_DIR})

and then STM32F4XX target should get it through linking to uart target, so you should be then able to do it almost as you wanted:

// because uart.h isn't located directly in `myproject/hardware/`, is it, as there is also `uart` subfolder
#include <hardware/uart/uart.h>

Note that if you are going to distribute your library/project, that won’t work for consuming project, as you’d need to set different paths with $<BUILD_INTERFACE:...>/$<INSTALL_INTERFACE:...> generator expressions.

Thanks for all the suggestions. I was trying it like pico-sdk for raspberry pi pico but that use really complex cmake so i guess i have to change the directory structure a bit to achieve what I really want