d我们在做自己工程的时候毕竟需要自己去创建.c和.h文件,但第一次用esp-idf,要写一点点cmake,其他和用keil之类的基本一样。下面直接开始。

准备工作 创建一个新工程 在esp-idf页面按ctrl + shift + p   输入: ESP-IDF:Welcome

点击 show example

点击 Use current 。。。。。

按下图点击

自己选择位置创建,就不截图了。

第一步 点击New File 创建两个新文件 命名test1.c和test1.h

创建之后目录结构就是这样

第二步 复制黏贴以下代码到test1.c

#include <stdio.h>
#include "test1.h"

void test_hello_world(int count)
{
    printf("Hello world! %d \n", ++count);
}

第三步 复制黏贴以下代码到test1.h

#ifndef _TEST1_H
#define _TEST1_H

void test_hello_world(int count);

#endif

第四步 复制以下代码到main.c

#include <stdio.h>
#include "test1.h"

void app_main(void)
{
    int count = 0;
    test_hello_world(count);
}

第五步  修改CMakeLists.txt

idf_component_register(SRCS "main.c" "test1.c"
                    INCLUDE_DIRS ".")

第六步 编译、烧录、打开监视器

第七步 查看结果

输出 Hello world! 1 则为正常

可以按ctrl + 】推出监视器。

这样就完全OK了,下面介绍创建文件夹中的工程

第一步 在刚才的基础上创建新的文件夹和文件,目录如下图、

第二步 把刚才的函数稍微改一点

test1.c改成这样

#include <stdio.h>
#include "test1.h"

void test1_hello_world(int *count)
{
    printf("Hello world! %d \n", ++(*count));
}

test1.h

#ifndef _TEST1_H
#define _TEST1_H

void test1_hello_world(int *count);

#endif

test2.c

#include <stdio.h>
#include "test2.h"

void test2_hello_world(int *count)
{
    printf("Hello world! %d \n", ++(*count));
}

test2.h

#ifndef _TEST2_H
#define _TEST2_H

void test2_hello_world(int *count);

#endif

main.c

#include <stdio.h>
#include "test1.h"
#include "test2.h"

void app_main(void)
{
    int count = 0;

    test1_hello_world(&count);
    test2_hello_world(&count);
}

第三步 修改CMakeLists.txt

idf_component_register(SRCS "main.c" "test1.c" "./test/test2.c"
                    INCLUDE_DIRS "." "./test")

第四步 编译、下载、打开监视器 和上面一样

结果如下

OK 结束!

22/7/4更新  这种方法简单粗暴无论idf4.4还是5.0都可以用,但是还是推荐大家用components的方法去搭建整个工程。工程写到后面肯定越来越大,不能一直往那个CMakeLists.txt里塞一堆文件名吧。具体方法idf4.4和5.0有区别,可以看一下官方文档,暂时懒得写了。

Migrate Build System to ESP-IDF 5.0 – ESP32 – — ESP-IDF Programming Guide latest documentation (espressif.com)



版权声明:本文为weixin_43410806原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_43410806/article/details/123774944