Building CMake project without

Posts: 6
Joined: Tue Feb 11, 2020 5:00 pm

Building CMake project without

Postby bluefarmer » Tue Feb 11, 2020 6:35 pm


I'm attempting to build a project using the esp-idf.
I've been using the script and the idf_component_register functions and that has worked however I'd like to switch to pure cmake to make debugging my own cmake files easier and to reduce the amount of 'magic' in the system a little bit.

I've tried using idf_build_process(esp32) instead of the script that seems to call cmake again in script mode which disallows certain cmake commands. My project structure is as follows:


Before running the cmake command I run the first to set any needed environment variables.
Then I cd into build and run cmake ..

Code: Select all

# root/project/CMakeLists.txt

cmake_minimum_required(VERSION 3.5...3.16)

Code: Select all

# root/project/main/CMakeLists.txt

#    SRCS "main.c"
add_executable(main.elf main.c)
target_link_libraries(main.elf PRIVATE custom)

Code: Select all

# root/project/components/custom/CMakeLists.txt

        custom STATIC
target_include_directories(custom PUBLIC ${CMAKE_CURRENT_LIST_DIR})
I get an error stating that the add_library() command is not scriptable which I think is because the idf_build_process runs cmake again in scripting mode. When I uncomment idf_component_register and comment out add_executable it passes that stage of the build at least but I was hoping to not need idf_component_register and use straight CMake like add_library and add_executable instead.

Any hints regarding this and how to better structure a cmake idf project are highly appreciated.


Posts: 2183
Joined: Sun May 08, 2016 4:11 am

Re: Building CMake project without

Postby ESP_Angus » Tue Feb 11, 2020 10:21 pm

Hi bluefarmer,

It should be possible to do something like what you want. I have a couple of pointers that should help you get things unstuck:


First is a clarification. You can still use the IDF build system and run "cmake" instead of "", the same way that you would to build a normal CMake project. is just a wrapper around cmake & make/ninja, plus some helpers for flashing. If you look at the logs from it prints the CMake commands that it runs in the background.

Some docs about running CMake directly (but still using IDF build system) can be found here: ... e-directly


Making a "pure CMake" project that doesn't use the IDF build system to build your source files is also possible, but if you do this then you can't can't include "IDF_PATH/tools/cmake/project.cmake" into your project because this overrides CMake's normal "project" function. If you want the generic CMake behaviour with no IDF "magic" in your project then you need to include "/tools/cmake/idf.cmake" instead.

You'll also need to move the add_executable() and idf_build_executable() steps out of "main" component and up into the top-level component. This is similar to how you'd structure a CMake build process for a single executable, outside of IDF..

These may be the only things which are preventing the CMakeLists files you have from building correctly, but if you have more trouble then I suggest checking against the info here: ... e-projects

and checking closely against the example here: ... idf_as_lib

Posts: 6
Joined: Tue Feb 11, 2020 5:00 pm

Re: Building CMake project without

Postby bluefarmer » Fri Feb 14, 2020 11:18 am

Including idf.cmake instead of project.cmake was my main problem.
Looking at those example projects and the build scripts helped me solve the other ones.


Who is online

Users browsing this forum: No registered users and 28 guests