# Building a Library

**URL:** <https://discourse.cmake.org/t/building-a-library/11350>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [July 30, 2024, 8:28am UTC](https://discourse.cmake.org/t/building-a-library/11350 "2024-07-30T08:28:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![undefined2001](https://discourse.cmake.org/user_avatar/discourse.cmake.org/undefined2001/32/4813_2.png) [@undefined2001](https://discourse.cmake.org/u/undefined2001)\
**Post date:** [July 30, 2024, 8:28am UTC](https://discourse.cmake.org/t/building-a-library/11350/1 "2024-07-30T08:28:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [July 30, 2024, 8:54am UTC](https://discourse.cmake.org/t/building-a-library/11350/2 "2024-07-30T08:54:45Z")

</div>

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](https://cmake.org/cmake/help/latest/guide/tutorial/index.html).

---

<div class="post-metadata">

**Author:** ![undefined2001](https://discourse.cmake.org/user_avatar/discourse.cmake.org/undefined2001/32/4813_2.png) [@undefined2001](https://discourse.cmake.org/u/undefined2001)\
**Post date:** [July 30, 2024, 10:50am UTC](https://discourse.cmake.org/t/building-a-library/11350/3 "2024-07-30T10:50:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [July 30, 2024, 11:24am UTC](https://discourse.cmake.org/t/building-a-library/11350/4 "2024-07-30T11:24:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![undefined2001](https://discourse.cmake.org/user_avatar/discourse.cmake.org/undefined2001/32/4813_2.png) [@undefined2001](https://discourse.cmake.org/u/undefined2001)\
**Post date:** [July 30, 2024, 11:28am UTC](https://discourse.cmake.org/t/building-a-library/11350/5 "2024-07-30T11:28:06Z")

</div>

```auto
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

```

```auto
# Add subdirectories for each peripheral
add_subdirectory(i2c)
add_subdirectory(uart)
add_subdirectory(clock)
add_subdirectory(spi)
#this is the cmakefile inside myproject/hardware

```

```auto
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"`

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [July 30, 2024, 11:51am UTC](https://discourse.cmake.org/t/building-a-library/11350/6 "2024-07-30T11:51:51Z")

</div>

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

```cmake
add_library(uart STATIC
  uart.c
  uart.h
)

```

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

```cmake
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:

```cmake
# 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:

```cpp
// 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.

---

<div class="post-metadata">

**Author:** ![undefined2001](https://discourse.cmake.org/user_avatar/discourse.cmake.org/undefined2001/32/4813_2.png) [@undefined2001](https://discourse.cmake.org/u/undefined2001)\
**Post date:** [July 30, 2024, 11:55am UTC](https://discourse.cmake.org/t/building-a-library/11350/7 "2024-07-30T11:55:10Z")

</div>

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
