element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Raspberry Pico and CMake - create your own C lib or subdirectory with header files
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 20 Nov 2022 10:11 PM Date Created
  • Views 9275 views
  • Likes 10 likes
  • Comments 5 comments
Related
Recommended
  • raspberry
  • pico
  • cmake

Raspberry Pico and CMake - create your own C lib or subdirectory with header files

Jan Cumps
Jan Cumps
20 Nov 2022

goal: "how to structure your project, while keeping the IDE happy and the build functionality simple"

When you're working with the Pico C SDK, you can add a library, like SPI, UART, … That will (seemingly) automatically add its include files to the path, and will compile its source files at build time. Very convenient, but how they do that? It's simple. If your project uses SPI, you usually add this to your CMake file:

target_link_libraries(<YOURPROJECT> pico_stdlib hardware_spi)

And this will take care that SPI libraries are linked into your firmware, but also that if you include #include "hardware/spi.h" in your source files, the build (and even a good IDE like VSCode) will actually find the header file.

This mechanism is used a lot in the C SDK and Pico examples. You can use it for your own projects too, to easily group functionality in subfolders, and to create your own utility libraries. In this post, I'm going to show how you can locate some functionality of your firmware in a subdirectory, and have the same functionality to simply add both sources and headers to your development setup. You will need a CMake file in the subdirectory, to make it self-registering. And two entries in the main CMake file of your project.

Example: M25XX Serial Flash submodule

My firmware will use a Micron M25P16 SPI flash IC. I want to put all m25 logic in a subdirectory, while trying to keep my build and IDE structure simple. This is a good starting point to start learning CMake modularity. I created a m25 subdirectory in my project. And created a header and c file in it, together with aCMakeList.txt file.

This example m25 lib will be very simple on purpose.

m25/m25.h

#ifndef _M25_H
#define _M25_H

int return25();

#endif // _MM25_H

The module m25/m25.c file:

#include "m25.h"

int return25() {
    return 25;
}

As you can see, this is the simplest possible C API - only intended as example.

The m25/CMakeList.txt file:

add_library( m25
    m25.h
    m25.c
    )

target_include_directories(m25 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

This will take care that the main CMake file knows what's part of the directory.

Using the M25XX submodule

Let's now use this subdirectory in our project. First add it to the ./CMakeList.txt file:

cmake_minimum_required(VERSION 3.13)

# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)

project(myproject C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

pico_sdk_init()

add_subdirectory(m25)

add_executable(myproject
        main.c
        )

target_link_libraries(myproject pico_stdlib m25)


pico_add_extra_outputs(myproject)

Now the main.c file:

#include "m25.h"

int main() {
    return return25();
}

Both the build system and the IDE will know what to do with the .h and .c file, and where they are. Your editor will be able to provide auto-complete, and show the API parameters and source. When you build it, the m25 sources will be included and linked. Your CMake main file does not have to know how your subdirectory is constructed and how many source files are in there. A value you'll get to appreciate when your project grows bigger.

  • Sign in to reply
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to flyingbean

    I use the same resource, and the CMakeList.txt files of the pico-sdk and pico-examples.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • flyingbean
    flyingbean over 2 years ago

    I am learning CMake now. Would you recommend some practical CMake reference for Raspberry Pi Pico? I am following this website for my CMake training now: cmake.org/.../index.html

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago

    Q: What if your subdirectory uses include files from the C SDK?

    If your CMake enabled subdirectory uses C SDK header files, such as SPI, ...., you will have to refer to them in the subdir's CMakeList.txt

    add_library( m25
        m25.h
        m25.cpp
        )
    
    target_link_libraries(m25
        hardware_clocks
        hardware_resets
        hardware_spi
    )
    
    target_include_directories(m25 PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 2 years ago

    Also, wasn't there a question on element14 somewhere recently, concerning e-books? Perhaps all the miscellaneous software tips blogs people produce could be wrapped up into a small monthly booklet. A bit like Dr Dobbs used to be, but obviously a lot slimmer. It would encourage people to use the tips in unrelated projects too, since, for instance, the tips here also apply to non-Pico projects too. If they were all in a booklet, people could browse through monthly.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 2 years ago

    Very useful! I'm slowly learning CMake project-by-project, and am glad that the Pico SDK based projects use CMake. Also the libraries are handy when saving projects, since you can then split up your project into separate repositories, and use the git clone submodule functionality to pull in the libraries, to simplify reuse in other projects.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube