具有多个源文件的 ESP-IDF 项目
ESP-IDF project with multiple source files
我从一个简单的“眨眼”示例开始我的项目,并将其用作编写代码的模板。
此示例仅使用了一个源文件 blink.c
。
最后,我想要一个使用多源文件的项目,但不知道如何配置 CMakeLists.txt
来编译项目。
我的CMakeLists.txt
是:
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(blink)
我想添加例如 init.c
。
我尝试了不同的方法,但没有成功。
None 的 idf_component_register() / register_component() 对我有用。
知道如何正确配置项目吗?
对,ESP IDF 中的 CMake 项目层次结构有点棘手。您正在查看错误的 CMakeLists.txt
文件。打开 blink/main/CMakeLists.txt
中的那个,而不是根目录中的那个。该文件列出了您要使用的“主要”组件的源文件。它看起来像这样:
idf_component_register(SRCS "blink.c" "init.c"
INCLUDE_DIRS ".")
确保您的 init.c
文件与此 CMakeLists.txt
和 blink.c
.
位于同一目录中
我也推荐看一下乐鑫Build System documentation,很有用
您应该编辑项目文件夹内 main
文件夹中的 CMakeLists.txt。另外,需要将包含头文件的目录放入INCLUDE_DIRS
参数中。
例如,如果您的项目中有此文件结构(您将 init.h
放在 include
文件夹中),如下所示:
blink/
├── main/
│ ├── include/
│ │ └── init.h
│ ├── blink.c
│ ├── CMakeLists.txt
│ ├── init.c
│ └── ...
├── CMakeLists.txt
└── ...
你的main/CMakeLists.txt
中的内容应该是:
idf_component_register(SRCS "blink.c" "init.c"
INCLUDE_DIRS "." "include")
我从一个简单的“眨眼”示例开始我的项目,并将其用作编写代码的模板。
此示例仅使用了一个源文件 blink.c
。
最后,我想要一个使用多源文件的项目,但不知道如何配置 CMakeLists.txt
来编译项目。
我的CMakeLists.txt
是:
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(blink)
我想添加例如 init.c
。
我尝试了不同的方法,但没有成功。
None 的 idf_component_register() / register_component() 对我有用。
知道如何正确配置项目吗?
对,ESP IDF 中的 CMake 项目层次结构有点棘手。您正在查看错误的 CMakeLists.txt
文件。打开 blink/main/CMakeLists.txt
中的那个,而不是根目录中的那个。该文件列出了您要使用的“主要”组件的源文件。它看起来像这样:
idf_component_register(SRCS "blink.c" "init.c"
INCLUDE_DIRS ".")
确保您的 init.c
文件与此 CMakeLists.txt
和 blink.c
.
我也推荐看一下乐鑫Build System documentation,很有用
您应该编辑项目文件夹内 main
文件夹中的 CMakeLists.txt。另外,需要将包含头文件的目录放入INCLUDE_DIRS
参数中。
例如,如果您的项目中有此文件结构(您将 init.h
放在 include
文件夹中),如下所示:
blink/
├── main/
│ ├── include/
│ │ └── init.h
│ ├── blink.c
│ ├── CMakeLists.txt
│ ├── init.c
│ └── ...
├── CMakeLists.txt
└── ...
你的main/CMakeLists.txt
中的内容应该是:
idf_component_register(SRCS "blink.c" "init.c"
INCLUDE_DIRS "." "include")