[英]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.h
和dosomething/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 文件所在的目錄。
我對cmake
和ESP-IDF
不太熟悉。 我在nvim
中使用platformio
並make
. 但嘗試在根目錄中創建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.