繁体   English   中英

包含子目录以在 ESP-IDF for ESP32 中组织代码

[英]Include subdirectories to organise code in ESP-IDF for ESP32

我正在使用 ESP-IDF 4.3.2 使用以下命令创建一个 ESP32 项目:

idf.py create-project --path test test

然后我修改 main/main.c 以包含以下代码:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

void app_main(void)
{
    int i = 0;
    while (1) {
        printf("[%d] Hello world!\n", i);
        i++;
        vTaskDelay(5000 / portTICK_PERIOD_MS);
    }
}

这编译并运行得很好,但是如果我尝试创建 main 的子目录来组织我的代码,我似乎遇到了错误。 我将添加dosomething/printsomething.hdosomething/printsomething.c内容如下:

dosomething/printsomething.h

void printsomething(void);

dosomething/printsomething.c

#include <stdio.h>

void printsomething(void)
{
    printf("something\n");
}

然后我修改main.c如下:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "dosomething/printsomething.h"

void app_main(void)
{
    int i = 0;
    while (1) {
        printf("[%d] Hello world!\n", i);
        printsomething();
        i++;
        vTaskDelay(5000 / portTICK_PERIOD_MS);
    }
}

代码构建失败,我看到“未定义对‘printsomething’的引用”错误。

/home/builduser/.espressif/tools/xtensa-esp32-elf/esp-2021r2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: esp-idf/main/libmain.a(test.c.obj):(.literal.app_main+0x4): undefined reference to `printsomething'
/home/builduser/.espressif/tools/xtensa-esp32-elf/esp-2021r2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: esp-idf/main/libmain.a(test.c.obj): in function `app_main':
/home/builduser/Documents/code/esp/esp/test/build/../main/test.c:11: undefined reference to `printsomething'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

我可以看到main\CMakeLists.txt文件包含以下内容:

idf_component_register(SRCS "test.c"
                    INCLUDE_DIRS ".")

但是我找不到 INCLUDE_DIRS 的正确语法,这将允许我指定 C 和 Header 文件所在的目录。

我对cmakeESP-IDF不太熟悉。 我在nvim中使用platformiomake . 但尝试在根目录中创建CMakeLists.txt包含:

PROJECT(test)

SET(DIRS dosomething main)
SUBDIRS(${DIRS})
INCLUDE_DIRECTORIES(${DIRS})

main目录CMakeLists.txt包含:

ADD_EXECUTABLE(test main.c)
TARGET_LINK_EXECUTABLE(test something)

dosomething目录中CMakeLists.txt包含:

ADD_LIBRARY(something printsomething.c)

为此有一个默认文件夹。 创建一个名为components的文件夹并将dosomething文件夹放在其中。 components文件夹不应在main中创建,而应与main位于同一目录中。 因此,您可以使用#include "printsomething.h"一切都会正常工作。 您无需修改CMakeLists.txt ,因为components是默认目录。 在这里查看更多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM