gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:09:20 29 4
gpt4 key购买 nike

我正在尝试编译此 UART -> USB driver来自硅实验室。它仅在 Ubuntu 版本 18.04 (Bionic) 上进行了测试,我的机器在 21.10 (Impish) 上运行。显然,差异之一是最新版本在构建内核模块时启用了严格的指针转换检查:

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

# enforce correct pointer usage
KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)

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

.../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source/cp210x.c:290:35: error: initialization of ‘void (*)(struct usb_serial_port *)’ from incompatible pointer type ‘int (*)(struct usb_serial_port *)’ [-Werror=incompatible-pointer-types]
290 | .port_remove = cp210x_port_remove,
| ^~~~~~~~~~~~~~~~~~
.../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source/cp210x.c:290:35: note: (near initialization for ‘cp210x_device.port_remove’)
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277: .../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source/cp210x.o] Error 1
make[1]: *** [Makefile:1874: .../vcp_driver_source/Linux_3.x.x_4.x.x_VCP_Driver_Source] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.15.23-76051523-generic'
make: *** [Makefile:7: all] Error 2

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

Makefile

obj-m = cp210x.o
KDIR = /lib/modules/`uname -r`/build
SRCDIR = $(PWD)
# try this instead if you don't have PWD defined
# SRCDIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
all:
$(MAKE) -C $(KDIR) M=$(SRCDIR) modules
clean:
$(MAKE) -C $(KDIR) M=$(SRCDIR) clean

最佳答案

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

ccflags-y := -Wno-error=incompatible-pointer-types

来自linux kernel documentation我找到了

--- 3.7 Compilation flags

ccflags-y, asflags-y and ldflags-yThese three flags apply only to the kbuild makefile in which theyare assigned. They are used for all the normal cc, as and ldinvocations happening during a recursive build.Note: Flags with the same behaviour were previously named:EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.They are still supported but their usage is deprecated.

ccflags-y specifies options for compiling with $(CC).

Example:

ccflags-y            := -Os -D_LINUX -DBUILDING_ACPICA
ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT

This variable is necessary because the top Makefile owns thevariable $(KBUILD_CFLAGS) and uses it for compilation flags for theentire tree.

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

关于c - 制作驱动程序时禁用 KBUILD_CFLAGS 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71300360/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com