简体   繁体   English

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

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

I'm using ESP-IDF 4.3.2 to create an ESP32 project with the following command:我正在使用 ESP-IDF 4.3.2 使用以下命令创建一个 ESP32 项目:

idf.py create-project --path test test

I then modify main/main.c to contain the following code:然后我修改 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);
    }
}

This compiles and runs just fine, but if I try to create a subdirectory of main to organise my code I seem to hit an error.这编译并运行得很好,但是如果我尝试创建 main 的子目录来组织我的代码,我似乎遇到了错误。 I'll add dosomething/printsomething.h and dosomething/printsomething.c with the following contents:我将添加dosomething/printsomething.hdosomething/printsomething.c内容如下:

dosomething/printsomething.h : dosomething/printsomething.h

void printsomething(void);

dosomething/printsomething.c : dosomething/printsomething.c

#include <stdio.h>

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

I then modify main.c as follows:然后我修改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);
    }
}

The code fails to build and I see an "undefined reference to `printsomething'" error.代码构建失败,我看到“未定义对‘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

I can see that the main\CMakeLists.txt file contains the following:我可以看到main\CMakeLists.txt文件包含以下内容:

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

But I can't find the right syntax for INCLUDE_DIRS which will allow me to specify the directories where the C and Header files are.但是我找不到 INCLUDE_DIRS 的正确语法,这将允许我指定 C 和 Header 文件所在的目录。

Im not exactly familiar with cmake and ESP-IDF .我对cmakeESP-IDF不太熟悉。 I use platformio in nvim and make .我在nvim中使用platformiomake . But try create in root directory CMakeLists.txt contains:但尝试在根目录中创建CMakeLists.txt包含:

PROJECT(test)

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

In main directory CMakeLists.txt contains:main目录CMakeLists.txt包含:

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

And in dosomething dir CMakeLists.txt contains:dosomething目录中CMakeLists.txt包含:

ADD_LIBRARY(something printsomething.c)

There is a default folder for this.为此有一个默认文件夹。 Create a folder called components and place the dosomething folder inside it.创建一个名为components的文件夹并将dosomething文件夹放在其中。 The components folder should not be created inside main , but in the same directory as main . components文件夹不应在main中创建,而应与main位于同一目录中。 So you can use #include "printsomething.h" and everything will work.因此,您可以使用#include "printsomething.h"一切都会正常工作。 You don't need to modify your CMakeLists.txt , as components is a default directory.您无需修改CMakeLists.txt ,因为components是默认目录。 See more here . 在这里查看更多。

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

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