在C中的头文件中定义外部函数(模块)

时间:2013-09-17 07:41:55

标签: c struct header extern

当我编译下面概述的程序时,我收到以下错误

[igor@localhost ~/I2C]$ make i2c_VIPER DEFINE=-DVIPER
gcc -g -Wall -D__USE_FIXED_PROTOTYPES__ -DVIPER -ansi -lusb   -c -o i2c.o i2c.c
In file included from i2c.c:9:
viperboard.h:120: error: expected ‘)’ before ‘*’ token
i2c.c: In function ‘main’:
i2c.c:32: error: ‘usb_dev’ undeclared (first use in this function)
i2c.c:32: error: (Each undeclared identifier is reported only once
i2c.c:32: error: for each function it appears in.)
i2c.c:33: warning: implicit declaration of function ‘i2c_VIPER’
make: *** [i2c.o] Error 1

我已经尝试了很多东西,或多或少半盲,以使它工作。我定义的struct parsed_CLI_I2C_t完美无瑕。没有编译错误。但是,当我尝试以同等方式使用struct usb_device中的<usb.h>时,编译器并不满意。 我做错了什么?

以下是相对详细的描述。

让我们从标准#include的代码片段开始。 usb.h&gt; &lt; - 链接到完整的头文件

/* Data types */
struct usb_device;
struct usb_bus;

struct usb_device {
  struct usb_device *next, *prev;

  char filename[PATH_MAX + 1];

  struct usb_bus *bus;

  struct usb_device_descriptor descriptor;
  struct usb_config_descriptor *config;

  void *dev;        /* Darwin support */

  u_int8_t devnum;

  unsigned char num_children;
  struct usb_device **children;
};

这是第一个本地头文件#include“viperboard.h”

struct parsed_CLI_I2C_t;
extern int  i2c_VIPER (struct usb_device **usb_dev, struct parsed_CLI_I2C_t **CLI_I2C_options);
extern bool OpenDevice(void);

这是第二个本地头文件#include“I2C.h”

typedef struct 
{
   char  *USB_board; 
   int    query;
   int    write_type;
} parsed_CLI_I2C_t;

extern int  parse_CLI_I2C_options (int argc, char *argv[], parsed_CLI_I2C_t **CLI_I2C_options);

主程序看起来像这样

/* all other standard include stuff skipped for brevity */
#include <usb.h>
#include "viperboard.h"
#include <stdbool.h>

#include "I2C.h"

int main(int argc, char *argv[])
{
   parsed_CLI_I2C_t *CLI_I2C_options;
   parse_CLI_I2C_options (argc, argv, &CLI_I2C_options);

   struct usb_device *usb_dev; 
   i2c_VIPER (&usb_dev, &CLI_I2C_options);
}

最后,这是外部模块

  

i2c_VIPER.c

/* all other standard include stuff skipped for brevity */
#include <usb.h>
#include "viperboard.h"     
#include <stdbool.h>

#include "I2C.h"

int  i2c_VIPER (struct usb_device **usb_dev, struct parsed_CLI_I2C_t **CLI_I2C_options )
{
   bool   connected;         /* True if the ViperBoard is connected */

   connected = OpenDevice();

   return(0);
}

这是

  

OpenDevice.c

 #include <stdbool.h>
 #include <usb.h>

 bool OpenDevice()  /* <----  this is line 11 */ 
 {
     usb_set_debug( 0 );

     /* Initialize USB library */
     usb_init( );

     etc etc etc 

     return true;
 }

=============================================== ========= 30分钟后:实施所有建议的更改 出现了另一种错误。

 OpenDevice.c:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘OpenDevice’
make: *** [OpenDevice.o] Error 1

1 个答案:

答案 0 :(得分:2)

这一行

usb_device *usb_dev;  /* this is line 32 */

将无法正常编译C程序,而不是C ++程序。在C结构中,结构不是像C ++中那样的自动类型。您需要使用struct关键字来声明结构:

struct usb_device *usb_dev;  /* this is line 32 */

您必须对使用结构的每个地方进行此更改,例如i2c_VIPER函数的声明和定义。

另请注意,要使bool类型生效,您需要添加<stdbool.h>