目的
工作需要,今年开个新坑ESP32。纯小白,很多地方也是自己摸索的,可能有写地方会误人子弟,看客们见谅。
环境
ESP-IDF v5.2.1
使用ESP-IDF的sample_example模板新建工程。目录如下:
文档的结构树是:
--项目目录:
--build
--components (这个是esp-idf帮我创建项目的时候,把esp-idf根目录下的组件库复制到项目的文件夹内产生的)
--main
--main.c
--cmakelists.txt
--User_components
--system (我自己创建的组件库 可以理解为 C51里面的外设头文件和外设.c)
--user_system.c
--user_system.h
--cmakelists.txt (自己编写的 cmakelists 每一个组件都需要一个单独的cmakelist)
--CMakelists.txt (项目文件夹下的cmakelists)
--sdkconfig
大致说一下就是项目目录下有一个cmakelists,mian文件夹内也有一个cmakelists,项目目录的cmakelists是cmake这个工具帮我们构建项目的入口(?)大致是这个意思。
在项目目录下的cmakelists.txt中:
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five 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.16)
set(EXTRA_COMPONENT_DIRS "./User_components/system")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(ts1)
比较重要的是这句:set(EXTRA_COMPONENT_DIRS "./User_components/system")
意思就是 设置外部组件的目录是 (相对项目目录cmakelists.txt位置) ./User_components/system
,如果不加这一句,那么就无法找到自己编写的组件的头文件。
在main.c中写:
#include <stdio.h>
#include