簡體   English   中英

EXPORT_SYMBOL 宏給出沖突類型錯誤

[英]EXPORT_SYMBOL macro is giving conflicting types error

我正在嘗試將符號導出到 kernel。 但我收到以下錯誤。 我的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;

只要您在 header 文件和源文件中都可以看到相同的定義,這通常不會產生任何問題。

但在我的例子中,在源文件(.c)中, UInt32被視為一個宏,並在預處理完成后擴展為unsigned int

但在 header 文件中, UInt32仍然是用戶定義的數據類型。 所以編譯器拋出了上述錯誤。

在檢查了預處理器階段 output(為此使用-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();
        ^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM