CMAKE 和 ESP IDF:如何将组件包含为子文件夹?

问题描述 投票:0回答:2

我的项目看起来像这样,

.components
...drivers
......usb
........usb.c
........usb.h
......lcd
......etc
.main
...main.c

在 main.c 中,我想将库包含为

#include "drivers/usb.h"
#include "drivers/lcd.h"

如何配置 CMake 以包含子文件夹,但将组件本身包含为文件夹?

我尝试将子文件夹包含为 PRIV_INCLUDE_DIRS, 但它的工作原理是这样的,

#include "usb/usb.h" 

而不是

#include "component/sourcefile.h"

esp-idf 组件(例如驱动程序和 FreeRTOS)能够实现这一目标,因此 好吧,只是不知道如何实现这种风格。

任何帮助表示赞赏!

cmake esp32 esp-idf espressif-idf
2个回答
1
投票

首先,如果要使用组件本身之外的头文件,在组件的

INCLUDE_DIRS
中调用
PRIV_INCLUDE_DIRS
时需要使用
idf_component_register(...)
而不是
CMakeLists.txt
。有关更多信息,请参阅 IDF 构建系统组件命令文档页面

要启用将头文件包含为

driver/xxx.h
,您可以执行以下操作:在每个
driver
usb
等中添加一个子文件夹
lcd
,将相应的头文件移动到那里并添加每个目录(
usb
lcd
等),使用
INCLUDE_DIRS
中的
idf_component_register(...)
。 你的文件树看起来像:

.components
...drivers
......usb
........usb.c
........driver
..........usb.h
......lcd
........driver
..........lcd.h (not present above, but requested in your code example)
......etc
.main
...main.c

事实上,ESP-IDF 本身的许多驱动程序标头都是这样做的。


0
投票

@StrawHat 谢谢您的帮助。

然而,在

ESP-IDF/components
中,每个组件都有不同的结构,但父目录中的
CmakeLists.txt
似乎具有将组件视为子文件夹的功能,如下所示,

# Add each component as a subdirectory, processing each component's CMakeLists.txt
foreach(component_target ${build_component_targets})
    __component_get_property(dir ${component_target} COMPONENT_DIR)
    __component_get_property(_name ${component_target} COMPONENT_NAME)
    __component_get_property(prefix ${component_target} __PREFIX)
    __component_get_property(alias ${component_target} COMPONENT_ALIAS)
    set(COMPONENT_NAME ${_name})
    set(COMPONENT_DIR ${dir})
    set(COMPONENT_ALIAS ${alias})
    set(COMPONENT_PATH ${dir}) # for backward compatibility only, COMPONENT_DIR is preferred
    idf_build_get_property(build_prefix __PREFIX)
    set(__idf_component_context 1)
    if(NOT prefix STREQUAL build_prefix)
        add_subdirectory(${dir} ${prefix}_${_name})
    else()
        add_subdirectory(${dir} ${_name})
    endif()
    set(__idf_component_context 0)
endforeach()

我无法让它发挥作用,也无法弄清楚它的作用,你能简单介绍一下吗?

© www.soinside.com 2019 - 2024. All rights reserved.