ESP32ESP-IDF项目文件结构-创新互联
使用ESP-IDF开发时,项目特指一个目录,其中包含了构建可执行应用程序所需的全部文件和配置,以及其他支持型文件,例如分区表、数据/文件系统分区和引导程序。
ESP-IDF 并不是项目的一部分,它独立于项目,通过 IDF_PATH 环境变量(保存 esp-idf 目录的路径)链接到项目,从而将 IDF 框架与项目分离。
- myProject/
- CMakeLists.txt
- sdkconfig
- components/
- component1/
- CMakeLists.txt
- Kconfig
- src1.c
- component2/
- CMakeLists.txt
- Kconfig
- src1.c
- include/
- component2.h
- main/
- src1.c
- src2.c
- build/
顶层项目 CMakeLists.txt 文件,这是 CMake 用于学习如何构建项目的主要文件,可以在这个文件中设置项目全局的 CMake 变量。顶层项目 CMakeLists.txt 文件会导入 esp-idf/tools/cmake/project.cmake 文件,由它负责实现构建系统的其余部分。该文件最后会设置项目的名称,并定义该项目
sdkconfig 项目配置文件,执行 idf.py menuconfig 时会创建或更新此文件,文件中保存了项目中所有组件(包括 ESP-IDF 本身)的配置信息。 sdkconfig 文件可能会也可能不会被添加到项目的源码管理系统中。
可选的 component 目录中包含了项目的部分自定义组件,并不是每个项目都需要这种自定义组件,但它组件有助于构建可复用的代码或者导入第三方(不属于 ESP-IDF)的组件。
main 目录是一个特殊的 伪组件,包含项目本身的源代码。main 是默认名称,CMake 变量 COMPONENT_DIRS 默认包含此组件,但您可以修改此变量。或者,您也可以在顶层 CMakeLists.txt 中设置 EXTRA_COMPONENT_DIRS 变量以查找其他指定位置处的组件。
build 目录是存放构建输出的地方,如果没有此目录,idf.py 会自动创建。CMake 会配置项目,并在此目录下生成临时的构建文件。随后,在主构建进程的运行期间,该目录还会保存临时目标文件、库文件以及最终输出的二进制文件。此目录通常不会添加到项目的源码管理系统中,也不会随项目源码一同发布。
参考
hello-world的项目结构分析- hello_world/
- main/
- CMakeLists.txt
- component.mk
- hello_world_main.c
- .gdbinit.ci
- CMakeLists.txt
- loadable_elf_example_test.py
- Makefile
- README.md
- sdkconfig
- sdkconfig.ci
main 目录
CMakeLists.txt 文件CMakeLists.txt内容
idf_component_register(SRCS "hello_world_main.c"
INCLUDE_DIRS "")
idf_component_register函数中SRC中包含所有的源文件,INCLUDE_DIRS中包含所有的头文件目录如果main中的文件结构这样
- main/
- include/
- hello_world.h
- light_control.h
- CMakeLists.txt
- component.mk
- hello_world_main.c
- light_control.c
CMakeLists.txt中的内容应该修改成
idf_component_register(SRCS "hello_world_main.c"
"light_control.c"
INCLUDE_DIRS "include")
component.mk 文件GUN Make中使用的文件,通过CMake构建时可以在CMakeLists.txt设置 COMPONENT_ADD_INCLUDEDIRS 和 COMPONENT_SRCDIRS 等变量将组件添加到编译过程中。
hello_world_main.c 文件开发的源文件
.gdbinit.ci 文件gdb初始化配置文件
CMakeLists.txtCMakeLists.txt 内容
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello-world)
cmake_minimum_required(VERSION 3.5) 必须放在 CMakeLists.txt 文件的第一行,它会告诉 CMake 构建该项目所需要的最小版本号。ESP-IDF 支持 CMake 3.5 或更高的版本。
include($ENV{IDF_PATH}/tools/cmake/project.cmake) 会导入 CMake 的其余功能来完成配置项目、检索组件等任务。
project(hello-world) 会创建项目本身,并指定项目名称。该名称会作为最终输出的二进制文件的名字,即 hello-world.elf 和 hello-world.bin。每个 CMakeLists 文件只能定义一个项目。
项目顶层 Makefile,该 Makefile 设置了 PROJECT_NAME 变量,还可以定义作用于整个项目的其它 make 变量(可选)。顶层 Makefile 会导入核心 Makefile 文件 $(IDF_PATH)/make/project.mk ,由它负责实现 ESP-IDF 构建系统的剩余部分。在CMake编译过中是不需要的。
README.md 文件项目说明文件,markdown文本。
sdkconfig它的功能和使用Make的KConfig文件类似,使用CMake编译时,从 sdkconfig 文件中加载项目配置信息,生成 sdkconfig.cmake 和 sdkconfig.h 文件,分别用在 CMake 和 C/C++ 中定义配置项。如果项目配置发生了更改,CMake 会自动重新运行,重新生成上述两个文件,接着重新配置项目。通过idf.py menuconfig可以修改配置项。
idf_component_register函数idf_component_register函数在/tools/cmake/component.cmake中被定义。
# idf_component_register
#
# @brief Register a component to the build, creating component library targets etc.
#
# @param[in, optional] SRCS (multivalue) list of source files for the component
# @param[in, optional] SRC_DIRS (multivalue) list of source directories to look for source files
# in (.c, .cpp. .S); ignored when SRCS is specified.
# @param[in, optional] EXCLUDE_SRCS (multivalue) used to exclude source files for the specified
# SRC_DIRS
# @param[in, optional] INCLUDE_DIRS (multivalue) public include directories for the created component library
# @param[in, optional] PRIV_INCLUDE_DIRS (multivalue) private include directories for the created component library
# @param[in, optional] LDFRAGMENTS (multivalue) linker script fragments for the component
# @param[in, optional] REQUIRES (multivalue) publicly required components in terms of usage requirements
# @param[in, optional] PRIV_REQUIRES (multivalue) privately required components in terms of usage requirements
# or components only needed for functions/values defined in its project_include.cmake
# @param[in, optional] REQUIRED_IDF_TARGETS (multivalue) the list of IDF build targets that the component only supports
# @param[in, optional] EMBED_FILES (multivalue) list of binary files to embed with the component
# @param[in, optional] EMBED_TXTFILES (multivalue) list of text files to embed with the component
# @param[in, optional] KCONFIG (single value) override the default Kconfig
# @param[in, optional] KCONFIG_PROJBUILD (single value) override the default Kconfig
function(idf_component_register)
set(options)
set(single_value KCONFIG KCONFIG_PROJBUILD)
set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" ${ARGN})
if(NOT __idf_component_context)
message(FATAL_ERROR "Called idf_component_register from a non-component directory.")
endif()
__component_check_target()
__component_add_sources(sources)
# Add component manifest to the list of dependencies
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${COMPONENT_DIR}/idf_component.yml")
# Create the final target for the component. This target is the target that is
# visible outside the build system.
__component_get_target(component_target ${COMPONENT_ALIAS})
__component_get_property(component_lib ${component_target} COMPONENT_LIB)
# Use generator expression so that users can append/override flags even after call to
# idf_build_process
idf_build_get_property(include_directories INCLUDE_DIRECTORIES GENERATOR_EXPRESSION)
idf_build_get_property(compile_options COMPILE_OPTIONS GENERATOR_EXPRESSION)
idf_build_get_property(c_compile_options C_COMPILE_OPTIONS GENERATOR_EXPRESSION)
idf_build_get_property(cxx_compile_options CXX_COMPILE_OPTIONS GENERATOR_EXPRESSION)
idf_build_get_property(common_reqs ___COMPONENT_REQUIRES_COMMON)
include_directories("${include_directories}")
add_compile_options("${compile_options}")
add_c_compile_options("${c_compile_options}")
add_cxx_compile_options("${cxx_compile_options}")
# Unfortunately add_definitions() does not support generator expressions. A new command
# add_compile_definition() does but is only available on CMake 3.12 or newer. This uses
# add_compile_options(), which can add any option as the workaround.
#
# TODO: Use add_compile_definitions() once minimum supported version is 3.12 or newer.
idf_build_get_property(compile_definitions COMPILE_DEFINITIONS GENERATOR_EXPRESSION)
add_compile_options("${compile_definitions}")
if(common_reqs) # check whether common_reqs exists, this may be the case in minimalistic host unit test builds
list(REMOVE_ITEM common_reqs ${component_lib})
endif()
link_libraries(${common_reqs})
idf_build_get_property(config_dir CONFIG_DIR)
# The contents of 'sources' is from the __component_add_sources call
if(sources OR __EMBED_FILES OR __EMBED_TXTFILES)
add_library(${component_lib} STATIC ${sources})
__component_set_property(${component_target} COMPONENT_TYPE LIBRARY)
__component_add_include_dirs(${component_lib} "${__INCLUDE_DIRS}" PUBLIC)
__component_add_include_dirs(${component_lib} "${__PRIV_INCLUDE_DIRS}" PRIVATE)
__component_add_include_dirs(${component_lib} "${config_dir}" PUBLIC)
set_target_properties(${component_lib} PROPERTIES OUTPUT_NAME ${COMPONENT_NAME} LINKER_LANGUAGE C)
__ldgen_add_component(${component_lib})
else()
add_library(${component_lib} INTERFACE)
__component_set_property(${component_target} COMPONENT_TYPE CONFIG_ONLY)
__component_add_include_dirs(${component_lib} "${__INCLUDE_DIRS}" INTERFACE)
__component_add_include_dirs(${component_lib} "${config_dir}" INTERFACE)
endif()
# Alias the static/interface library created for linking to external targets.
# The alias is the::name.
__component_get_property(component_alias ${component_target} COMPONENT_ALIAS)
add_library(${component_alias} ALIAS ${component_lib})
# Perform other component processing, such as embedding binaries and processing linker
# script fragments
foreach(file ${__EMBED_FILES})
target_add_binary_data(${component_lib} "${file}" "BINARY")
endforeach()
foreach(file ${__EMBED_TXTFILES})
target_add_binary_data(${component_lib} "${file}" "TEXT")
endforeach()
if(__LDFRAGMENTS)
__ldgen_add_fragment_files("${__LDFRAGMENTS}")
endif()
# Set dependencies
__component_set_all_dependencies()
# Make the COMPONENT_LIB variable available in the component CMakeLists.txt
set(COMPONENT_LIB ${component_lib} PARENT_SCOPE)
# COMPONENT_TARGET is deprecated but is made available with same function
# as COMPONENT_LIB for compatibility.
set(COMPONENT_TARGET ${component_lib} PARENT_SCOPE)
__component_set_properties()
endfunction()
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
文章题目:ESP32ESP-IDF项目文件结构-创新互联
网页地址:http://scyanting.com/article/pciph.html