我正在尝试将一个符号导出到内核。但是我得到了下面的错误。我的linux版本是5.4.2,
/home/ram/checkout/drivers/char/i2c_sw_hw_common.c: At top level:
/home/ram/checkout/drivers/char/i2c_sw_hw_common.c:1031:14: error: conflicting types for ‘sfp_i2c_in32’
UInt32 sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType)
^~~~~~~~~~~~
In file included from /home/ram/checkout/drivers/char/i2c_sw_hw_common.c:3:
/home/ram/checkout/drivers/char/i2c_sw_hw_common.h:123:8: note: previous declaration of ‘sfp_i2c_in32’ was here
UInt32 sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType);
^~~~~~~~~~~~
/home/ram/checkout/drivers/char/i2c_sw_hw_common.c: In function ‘sfp_i2c_in32’:
/home/ram/checkout/drivers/char/i2c_sw_hw_common.c:1035:18: warning: unused variable ‘byte_count’ [-Wunused-variable]
unsigned int byte_count = 0 ;
^~~~~~~~~~
/home/ram/checkout/drivers/char/i2c_sw_hw_common.c: At top level:
/home/ram/checkout/drivers/char/i2c_sw_hw_common.c:1063:29: error: conflicting types for ‘sfp_i2c_in32’
EXPORT_SYMBOL_NOVERS(sfp_i2c_in32);
^~~~~~~
In file included from /home/ram/checkout/drivers/char/i2c_sw_hw_common.c:3:
/home/ram/checkout/drivers/char/i2c_sw_hw_common.h:123:8: note: previous declaration of ‘sfp_i2c_in32’ was here
UInt32 sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType);
^~~~~~~~~~~~
cc1: some warnings being treated as errors这是我对这个符号的声明、定义和导出。
i2c_sw_hw_common.c
UInt32 sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType)
{
// code
}
EXPORT_SYMBOL(sfp_i2c_in32);i2c_sw_hw_common.h
UInt32 sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType);发布于 2021-12-27 14:48:04
问题在于如何使用用户定义的数据类型。我没有使用unsigned int,而是使用了用户定义的UInt32。
typedef unsigned long UInt32;这通常不会造成任何问题,只要在头文件和源文件中都可以看到相同的定义。
但是在我的例子中,在源文件(.c)中,UInt32被当作宏处理,在预处理完成后被扩展为unsigned int。
但是在头文件中,UInt32仍然是用户定义的数据类型。所以编译器抛出了上面的错误。
在检查了预处理阶段的输出后,我了解了这一点(为此使用-save-temps标志)。
在预处理阶段之后,声明仍然是,
UInt32 sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType);但定义改为,
unsigned int sfp_i2c_in32(char_dev_t *dev,unsigned int I2cDevaddr, int alen, unsigned int offset,unsigned int I2cAddr,int Width, int AccessType)
{
// code
}因此,从我的代码中删除所有用户定义的数据类型,并且只使用预定义的数据类型。
以上问题的极简例子可以是,
// file.h
typedef unsigned long UInt32;
UInt32 f();
// file.c
#include "x.h"
#define UInt32 unsigned int
UInt32 f()
{
return 0;
}你会犯错误,
x.c:6:14: error: conflicting types for ‘f’
UInt32 f()
^
In file included from x.c:2:0:
x.h:3:8: note: previous declaration of ‘f’ was here
UInt32 f();
^https://stackoverflow.com/questions/70388244
复制相似问题