制作驱动程序时禁用 KBUILD_CFLAGS 选项

ric*_*i90 2 c makefile kernel-module linux-kernel kbuild

我正在尝试从芯片实验室编译这个 UART -> USB驱动程序。它只在 Ubuntu 版本上进行了测试18.04 (Bionic),我的机器运行在21.10 (Impish). 显然,差异之一是最新版本在构建内核模块时启用了严格的指针转换检查:

\n

/lib/modules/$(uname -r)/build/Makefile

\n
# enforce correct pointer usage\nKBUILD_CFLAGS   += $(call cc-option,-Werror=incompatible-pointer-types)\n
Run Code Online (Sandbox Code Playgroud)\n

我想知道是否有办法禁用该特定标志,因为它阻止我编译驱动程序。我收到错误:

\n
.../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source/cp210x.c:290:35: error: initialization of \xe2\x80\x98void (*)(struct usb_serial_port *)\xe2\x80\x99 from incompatible pointer type \xe2\x80\x98int (*)(struct usb_serial_port *)\xe2\x80\x99 [-Werror=incompatible-pointer-types]\n  290 |         .port_remove            = cp210x_port_remove,\n      |                                   ^~~~~~~~~~~~~~~~~~\n.../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source/cp210x.c:290:35: note: (near initialization for \xe2\x80\x98cp210x_device.port_remove\xe2\x80\x99)\ncc1: some warnings being treated as errors\nmake[2]: *** [scripts/Makefile.build:277: .../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source/cp210x.o] Error 1\nmake[1]: *** [Makefile:1874: .../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source] Error 2\nmake[1]: Leaving directory \'/usr/src/linux-headers-5.15.23-76051523-generic\'\nmake: *** [Makefile:7: all] Error 2\n
Run Code Online (Sandbox Code Playgroud)\n

Makefile很简单,我可以根据需要更改它

\n

生成文件

\n
obj-m = cp210x.o\nKDIR = /lib/modules/`uname -r`/build\nSRCDIR = $(PWD)\n# try this instead if you don\'t have PWD defined\n# SRCDIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))\nall:\n    $(MAKE) -C $(KDIR) M=$(SRCDIR) modules\nclean:\n    $(MAKE) -C $(KDIR) M=$(SRCDIR) clean\n
Run Code Online (Sandbox Code Playgroud)\n

ric*_*i90 5

将以下内容添加到 makefile 的顶部修复了问题

ccflags-y := -Wno-error=incompatible-pointer-types
Run Code Online (Sandbox Code Playgroud)

linux内核文档我能够找到

--- 3.7 编译标志

ccflags-y、asflags-y 和 ldflags-y 这三个标志仅适用于分配它们的 kbuild makefile。它们用于递归构建期间发生的所有正常 cc、as 和 ld 调用。注意:具有相同行为的标志以前被命名为:EXTRA_CFLAGS、EXTRA_AFLAGS 和 EXTRA_LDFLAGS。它们仍然受支持,但已弃用。

cflags-y 指定使用 $(CC) 进行编译的选项。

例子:

ccflags-y            := -Os -D_LINUX -DBUILDING_ACPICA
ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT
Run Code Online (Sandbox Code Playgroud)

该变量是必需的,因为顶层 Makefile 拥有变量 $(KBUILD_CFLAGS) 并将其用作整个树的编译标志。

之后只需找到正确的 gcc 标志即可。-Werror值得庆幸的是 gcc 提供了一种使用 取消设置标志的方法-Wno-error。覆盖默认行为并允许编译正常工作的设置。