0

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

[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_devicefrom<usb.h>时,编译器并不满意。我做错了什么?

下面进行比较详细的说明。

让我们从标准#include< usb.h > <-- 链接到完整头文件的代码片段开始

/* 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);
}

这是

开放设备.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
4

1 回答 1

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>.

于 2013-09-17T07:46:36.500 回答