ESP32 快速入门(四): CMake 以及分解 esp-idf 中的 hello_world 工程结构

一. CMake 简介
CMake 是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的 makefile 或者 project 文件,能测试编译器所支持的 C++ 特性,类似 UNIX 下的 automake。

二. CMake 示例及语法
以下是我选取的 CMake 代码示例:

[cpp] view plain copy
#project name
PROJECT(test_math)
#head file path
INCLUDE_DIRECTORIES( include )
#source directory
AUX_SOURCE_DIRECTORY(src DIR_SRCS)
#set environment variable
SET(TEST_MATH ${DIR_SRCS} )
#set extern libraries
SET(LIBRARIES libm.so )
#add executable file
ADD_EXECUTABLE(…/bin/bin ${TEST_MATH})
#add link library
TARGET_LINK_LIBRARIES(…/bin/bin ${LIBRARIES}

由此我们可以总结出对应的 CMake 语法:

PROJECT( project name ) 工程名称
INCLUDE_DIRECTORIES( include ) 头文件所在路径
SET( TEST_DIR ${DIR_SRCS}) 设置环境变量
SET(LIBRARIES libm.so) 设置外部库
ADD_EXECUTABLE( …/bin/bin ${TEST_DIR}) 设置可执行文件路径
TARGET_LINK_LIBRARIES(…/bin/bin ${LIBRARIES}) 设置链接库
ADD_SUBDIRECTORY 设置代码子目录
三. esp-idf 中 hello_world 项目结构简析
我们打开 esp-idf 中的 hello_world 工程,打开终端,使用 cd 命令进入 esp-idf/examples/get-started/hello_world,然后在终端使用 tree 查看此项目当前的结构:

├── CMakeLists.txt
├── main
│ ├── CMakeLists.txt
│ ├── component.mk
│ └── hello_world_main.c
├── Makefile
├── README.md
├── sdkconfig
└── sdkconfig.old

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)

我们可以看到:

CMakeLists.txt (CMake 配置文件)
main (工程主代码目录)
CMakeLists.txt (main 文件夹下的的 CMake 配置文件)
component.mk (main 组件的配置文件)
hello_world_main.c (hello_world 的主要代码)
Makefile(make 配置文件)
README.md
sdkconfig (通过 make menuconfig 生成的配置文件)
sdkconfig.old(通过 make menuconfig 生成的备份配置文件)
然后我们可以将工程结构相关的文件一一拿出来讨论。

  1. CMakeLists.txt
    文件里的内容如下:

#The following lines of boilerplate have to be in your project’s
#CMakeLists in this exact order for cmake to work correctly
#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 加载的配置文件,首先通过 cmake_minimum_required(VERSION 3.5) 定义了 CMake 所支持的最小版本。然后使用 include($ENV{IDF_PATH}/tools/cmake/project.cmake) 来包含 esp-idf 下的 project.cmake 文件,这个文件包含许多需要加载的组件,如果想深入了解,可以自行去查看。最后采取 project(hello-world) 来说明工程名为 hello-world。

  1. Makefile
    文件里的内容如下:

#This is a project Makefile. It is assumed the directory this Makefile resides in is a
#project subdirectory.

PROJECT_NAME := hello-world

include $(IDF_PATH)/make/project.mk

Makefile 文件是 make 命令加载的配置文件, 内容同 CMakeLists.txt 基本一致,PROJECT_NAME := hello-world 说明工程名为 hello-world,include $(IDF_PATH)/make/project.mk 用来包含 esp-idf 下的 project.cmake 文件。

  1. main
    main 文件夹用来放置工程主要源文件。在这个工程里有三个文件,分别是:

CMakeLists.txt
文件里的内容如下:
set(COMPONENT_SRCS “hello_world_main.c”)
set(COMPONENT_ADD_INCLUDEDIRS “”)

register_component()

CMakeLists.txt 用来编译 main 组件的配置,可以发现 set(COMPONENT_SRCS “hello_world_main.c”) 和 set(COMPONENT_ADD_INCLUDEDIRS “”) 用来设置相关源码的位置,最后通过 register_component() 注册组件。
component.mk
文件里的内容如下:

#“main” pseudo-component makefile.

#(Uses default behaviour of compiling all source files in directory, adding ‘include’ to include path.)

component.mk 用来编译 main 组件,我们可以发现这是个空文件,在这种情况下会默认编译该目录下的所有代码。
hello_world_main.c
此文件里的内容由于与构建工程无关,省略至如下:
#include <stdio.h>
#include “freertos/FreeRTOS.h”
#include “freertos/task.h”
#include “esp_system.h”
#include “esp_spi_flash.h”

void app_main()
{
printf(“Hello world!\n”);
}

hello_world_main.c 是项目比较核心的文件,我们可以在里面修改代码满足我们的相关需求。
四. 项目结构的要点总结及展望
通过本文的探索,我们可以总结出以下要点:

Makefile 文件应该放在项目根目录,一般情况下该文件所在目录会被编译系统自动保存为 PROJECT_NAME
sdkconfig 文件由 make menuconfig 自动生成,同级目录还会自动生成 sdkconfig.old、sdkconfig.defaults,这两个文件可以用于恢复上一次设置或默认设置
main 目录放置工程主要源文件,该目录被官方定义为“伪组件”,和组件目录遵循相同的编译规则。
最后,在更大型的工程中,我们可以发现工程中会存在 components 目录。以下是我搜索到的部分要点:

在 components 目录下可以不设置,构建系统会自动从 IDF_PATH 目录调用 component 源代码,如果将要使用到的 component 复制到工程目录。构建系统会使用当前目录下的 component,不影响 IDF_PATH 中的源代码。

可以使用 Kconfig 来声明 menuconfig 的选项,通过界面交互修改组件的宏定义。component.mk 用于配置组件的各种行为。
————————————————
版权声明:本文为CSDN博主「BigZombieZ」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zztiger123/article/details/103317796

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/home/fujiayu/esp/esp-idf/tools/check_python_dependencies.py:12: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html import pkg_resources Executing action: flash Running ninja in directory /home/fujiayu/esp/hello_world/build Executing "ninja flash"... [1/5] cd /home/fujiayu/esp/hello_world.../esp/hello_world/build/hello_world.bin hello_world.bin binary size 0x31ee0 bytes. Smallest app partition is 0x100000 bytes. 0xce120 bytes (80%) free. [1/1] cd /home/fujiayu/esp/hello_world..._world/build/bootloader/bootloader.bin Bootloader binary size 0x5290 bytes. 0x2d70 bytes (35%) free. [2/3] cd /home/fujiayu/esp/esp-idf/com...nents/esptool_py/run_serial_tool.cmake esptool esp32s3 -p /dev/ttyACM0 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 80m --flash_size 2MB 0x0 bootloader/bootloader.bin 0x10000 hello_world.bin 0x8000 partition_table/partition-table.bin esptool.py v4.6.2 Serial port /dev/ttyACM0 A fatal error occurred: Could not open /dev/ttyACM0, the port doesn't exist CMake Error at run_serial_tool.cmake:66 (message): /home/fujiayu/.espressif/python_env/idf5.2_py3.8_env/bin/python;;/home/fujiayu/esp/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32s3 failed. FAILED: CMakeFiles/flash cd /home/fujiayu/esp/esp-idf/components/esptool_py && /usr/bin/cmake -D IDF_PATH=/home/fujiayu/esp/esp-idf -D "SERIAL_TOOL=/home/fujiayu/.espressif/python_env/idf5.2_py3.8_env/bin/python;;/home/fujiayu/esp/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32s3" -D "SERIAL_TOOL_ARGS=--before=default_reset;--after=hard_reset;write_flash;@flash_args" -D WORKING_DIRECTORY=/home/fujiayu/esp/hello_world/build -P /home/fujiayu/esp/esp-idf/components/esptool_py/run_serial_tool.cmake ninja: build stopped: subcommand failed. ninja failed with exit code 1, output of the command is in the /home/fujiayu/esp/hello_world/build/log/idf_py_stderr_output_21690 and /home/fujiayu/esp/hello_world/build/log/idf_py_stdout_output_21690
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值