在基于zephyr的开发中,如果我们对自己的应用程序使用严格的编译选项,如-Wconversion,会导致zephyr系统的头文件产生很多编译错误,例如
为了解决这些编译错误,我们可以逐一对这些错误进行修改,这样可以保证整个编译过程应用统一的编译选项,但是修改过程比较繁琐。
更简单的方法是通过修改编译文件,忽略对系统头文件的错误检查。在通用的CMake文件中,我们可以通过-isystem,或target_include_directories中的SYSTEM参数将头文件声明为系统头文件,从而忽略对这些文件的错误检查。在zephyr中,zephyr_system_include_directories函数同样提供了添加系统头文件的方法。
以下代码给出了zephyr_include_directories和zephyr_system_include_directories两个函数的实现方式。
参考zephyr\cmake\modules\extensions.cmake
# https://cmake.org/cmake/help/latest/command/target_include_directories.html
function(zephyr_include_directories)
target_include_directories(zephyr_interface INTERFACE ${ARGV})
endfunction()
# https://cmake.org/cmake/help/latest/command/target_include_directories.html
function(zephyr_system_include_directories)
target_include_directories(zephyr_interface SYSTEM INTERFACE ${ARGV})
endfunction()
从代码中不难看出,zephyr_system_include_directories函数实际调用了target_include_directories函数,并携带SYSTEM参数,从而将新添加的头文件声明为系统头文件。
因此,对于zephyr系统头文件中的编译错误,我们只需要将zephyr目录下CMakeList.txt文件中以下代码改为zephyr_system_include_directories,即可忽略这些编译错误。
zephyr_include_directories(
include
${PROJECT_BINARY_DIR}/include/generated
${USERINCLUDE}
${STDINCLUDE}
)