概述
本项目通过Cmake构建。
一、用到的Linux操作指令
//切换目录
cd 目录
//新建文件夹。 p 确保目录名称存在,不存在的就建一个。
mkdir -p 文件夹名
//删除文件夹及里面所有文件
rm -rf 目录名字
//新建文件
touch 文件名.后缀
///
//vim 操作
//打开文件
vim 文件名.后缀
//编辑文件:键盘按“i“
//保存文件:键盘按“ESC“,然后输入“:wq“
二、建立项目
1、要建立的项目结构
/*
* - my_tamplate/
* - CMakeLists.txt
* - main/ - CMakeLists.txt
* - main.c
* - include/ - main.h
* - components/ - component1/ - CMakeLists.txt
* - comp1.c
* - include/ - comp1.h
*/
- my_tamplate 为项目文件夹
- CMakeLists.txt 为 项目 CMakeLists 文件,包含整个项目的构建设置
- main文件夹 为主程序文件夹
- CMakeLists.txt 为 组件 CMakeLists 文件
- main.c 为 主源程序
- include/ - main.h 为 头文件
- components 为 组件文件夹
- component1 为 组件成员文件夹,与 main文件夹 相似。
2、通过 终端 建立项目 (目的是练习一下linux操作)
- 按 ctrl + alt + T 打开终端
- 创建文件夹
mkdir -p ~/esp/myPrj/my_tamplate/main/include
mkdir -p ~/esp/myPrj/my_tamplate/components/component1/include
- 创建 项目 CMakeLists 文件
cd ~/esp/myPrj/my_tamplate
touch CMakeLists.txt
vim CMakeLists.txt
- 键盘按 “i” 进入编辑模式:
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my_tamplate)
- 键盘按ESC,然后输入“:wq”,进行保存
- 创建 main文件夹 里的文件
cd ~/esp/myPrj/my_tamplate/main
touch CMakeLists.txt
touch main.c
cd ~/esp/myPrj/my_tamplate/main/include
touch main.h
- 编辑 CMakeLists.txt
cd ~/esp/myPrj/my_tamplate/main
vim CMakeLists.txt
- 方法同上,编辑内容为:
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "include")
- 创建 component1文件夹 里的文件
cd ~/esp/myPrj/my_tamplate/components/component1
touch CMakeLists.txt
touch comp1.c
cd ~/esp/myPrj/my_tamplate/components/component1/include
touch comp1.h
- 编辑 CMakeLists.txt
cd ~/esp/myPrj/my_tamplate/components/component1
vim CMakeLists.txt
- 编辑内容为:
idf_component_register(SRCS "comp1.c"
INCLUDE_DIRS "include")
- 最后得到的项目结构:
└── my_tamplate
├── CMakeLists.txt
├── components
│ └── component1
│ ├── CMakeLists.txt
│ ├── comp1.c
│ └── include
│ └── comp1.h
└── main
├── CMakeLists.txt
├── include
│ └── main.h
└── main.c
.c和.h文件,在vscode里再编辑。
3、在VScode编译项目
- 使用 VScode 打开项目
cd ~/esp/myPrj
code my_tamplate
- 编辑 comp1.h
#ifndef _COMP1_H_
#define _COMP1_H_
void comp1(void);
#endif //_COMP1_H_
- 编辑 comp1.c
#include <stdio.h>
#include "comp1.h"
void comp1(void)
{
printf("Hello world!\n");
}
- 编辑 main.c
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "comp1.h"
void app_main(void)
{
//调用组件,串口输出helloworld
comp1();
//延时10秒
vTaskDelay(10000 / portTICK_PERIOD_MS);
//重启
esp_restart();
}
- 编译项目: ctrl+E, 然后B
- 烧录程序: ctrl+E, 然后F
- 监控串口:ctrl+E, 然后M
- 退出监控: ctrl+]
4、补全 VScode头文件路径
- 点击 灯泡, 选择 编辑“includePath” 。
- vscode自动生成,点击进入。
- 添加路径
"${workspaceFolder}/**",
"~/esp/esp-idf/components/freertos/include/",
"~/esp/esp-idf/components/esp_system/include/"
注:补全路径,是为了写代码的时候能补全代码;或者查看源文件。不补全路径,vscode会提示错误,但不影响编译!
- VScode关闭错误提示。点击 灯泡, 选择 ”禁用错误波形曲线“ 。
- VScode 启用 错误波形曲线。将对应内容删除即可。