Chapter 4. API Guides
For example, in the foo/CMakeLists.txt file:
add_library(foo bar.c fizz.cpp buzz.cpp)
if(ESP_PLATFORM)
# On ESP-IDF, bar.c needs to include esp_spi_flash.h from the spi_flash component
target_link_libraries(foo PRIVATE idf::spi_flash)
endif()
4.4.19 Using Prebuilt Libraries with Components
Another possibility is that you have a prebuilt static library (.a file), built by some other build process.
The ESP-IDF build system provides a utility function add_prebuilt_library for users to be able to easily
import and use prebuilt libraries:
add_prebuilt_library(target_name lib_path [REQUIRES req1 req2 ...] [PRIV_REQUIRES␣
,→req1 req2 ...])
where:
• target_name- name that can be used to reference the imported library, such as when linking to other targets
• lib_path- path to prebuilt library; may be an absolute or relative path to the component directory
Optional arguments REQUIRES and PRIV_REQUIRES specify dependency on other components. These have the
same meaning as the arguments for idf_component_register.
Take note that the prebuilt library must have been compiled for the same target as the consuming project. Configu-
ration relevant to the prebuilt library must also match. If not paid attention to, these two factors may contribute to
subtle bugs in the app.
For an example, take a look at build_system/cmake/import_prebuilt.
4.4.20 Using ESP-IDF in Custom CMake Projects
ESP-IDF provides a template CMake project for easily creating an application. However, in some instances the user
might already have an existing CMake project or may want to create a custom one. In these cases it is desirable to be
able to consume IDF components as libraries to be linked to the user’s targets (libraries/ executables).
It is possible to do so by using the build system APIs provided by tools/cmake/idf.cmake. For example:
cmake_minimum_required(VERSION 3.5)
project(my_custom_app C)
# Include CMake file that provides ESP-IDF CMake build system APIs.
include($ENV{IDF_PATH}/tools/cmake/idf.cmake)
# Include ESP-IDF components in the build, may be thought as an equivalent of
# add_subdirectory() but with some additional processing and magic for ESP-IDF␣
,→build
# specific build processes.
idf_build_process(esp32)
# Create the project executable and plainly link the newlib component to it using
# its alias, idf::newlib.
add_executable(${CMAKE_PROJECT_NAME}.elf main.c)
target_link_libraries(${CMAKE_PROJECT_NAME}.elf idf::newlib)
# Let the build system know what the project executable is to attach more targets,␣
,→dependencies, etc.
idf_build_executable(${CMAKE_PROJECT_NAME}.elf)
Espressif Systems 1290
Submit Document Feedback
Release v4.4