簡體   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