EXPORT_SYMBOL 宏给出类型冲突错误

EXPORT_SYMBOL macro is giving conflicting types error

我正在尝试将符号导出到内核。但我收到以下错误。我的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);

问题在于使用用户定义的数据类型。我使用了用户定义的 UInt32 而不是 unsigned int

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();
        ^