我正在尝试编译可能正常工作代码的代码。我收到了几个错误;
joystick.c: In function ‘open_joystick’:
joystick.c:14:1: error: number of arguments doesn’t match prototype
joystick.h:45:12: error: prototype declaration
joystick.c: In function ‘get_joystick_status’:
joystick.c:57:16: error: ‘struct wwvi_js_event’ has no member named ‘stick1_x’
joystick.c:59:16: error: ‘struct wwvi_js_event’ has no member named ‘stick1_y’
joystick.c:61:16: error: ‘struct wwvi_js_event’ has no member named ‘stick2_x’
joystick.c:63:16: error: ‘struct wwvi_js_event’ has no member named ‘stick2_y’
joystick.c: In function ‘open_joystick’:
joystick.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
这是代码
Joystick.h:
/*
(C) Copyright 2007,2008, Stephen M. Cameron.
This file is part of wordwarvi.
wordwarvi is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
wordwarvi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wordwarvi; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#ifndef __JOYSTICK_H__
#define __JOYSTICK_H__
#define JOYSTICK_DEVNAME "/dev/input/js0"
#define JS_EVENT_BUTTON 0x01 /* button pressed/released */
#define JS_EVENT_AXIS 0x02 /* joystick moved */
#define JS_EVENT_INIT 0x80 /* initial state of device */
struct js_event {
unsigned int time; /* event timestamp in milliseconds */
short value; /* value */
unsigned char type; /* event type */
unsigned char number; /* axis/button number */
};
struct wwvi_js_event {
int button[11];
int stick_x;
int stick_y;
};
extern int open_joystick(char *joystick_device);
extern int read_joystick_event(struct js_event *jse);
extern void set_joystick_y_axis(int axis);
extern void set_joystick_x_axis(int axis);
extern void close_joystick();
extern int get_joystick_status(struct wwvi_js_event *wjse);
#endif
Joystick.C:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "joystick.h"
static int joystick_fd = -1;
int open_joystick()
{
joystick_fd = open(JOYSTICK_DEVNAME, O_RDONLY | O_NONBLOCK); /* read write for force feedback? */
if (joystick_fd < 0)
return joystick_fd;
/* maybe ioctls to interrogate features here? */
return joystick_fd;
}
int read_joystick_event(struct js_event *jse)
{
int bytes;
bytes = read(joystick_fd, jse, sizeof(*jse));
if (bytes == -1)
return 0;
if (bytes == sizeof(*jse))
return 1;
printf("Unexpected bytes from joystick:%d\n", bytes);
return -1;
}
void close_joystick()
{
close(joystick_fd);
}
int get_joystick_status(struct wwvi_js_event *wjse)
{
int rc;
struct js_event jse;
if (joystick_fd < 0)
return -1;
// memset(wjse, 0, sizeof(*wjse));
while ((rc = read_joystick_event(&jse) == 1)) {
jse.type &= ~JS_EVENT_INIT; /* ignore synthetic events */
if (jse.type == JS_EVENT_AXIS) {
switch (jse.number) {
case 0: wjse->stick1_x = jse.value;
break;
case 1: wjse->stick1_y = jse.value;
break;
case 2: wjse->stick2_x = jse.value;
break;
case 3: wjse->stick2_y = jse.value;
break;
default:
break;
}
} else if (jse.type == JS_EVENT_BUTTON) {
if (jse.number < 10) {
switch (jse.value) {
case 0:
case 1: wjse->button[jse.number] = jse.value;
break;
default:
break;
}
}
}
}
// printf("%d\n", wjse->stick1_y);
return 0;
}
#if 0
/* a little test program */
int main(int argc, char *argv[])
{
int fd, rc;
int done = 0;
struct js_event jse;
fd = open_joystick();
if (fd < 0) {
printf("open failed.\n");
exit(1);
}
while (!done) {
rc = read_joystick_event(&jse);
usleep(1000);
if (rc == 1) {
printf("Event: time %8u, value %8hd, type: %3u, axis/button: %u\n",
jse.time, jse.value, jse.type, jse.number);
}
}
}
#endif
任何人都可以告诉我错误的原因是我一直试图调试它半小时现在无济于事。
答案 0 :(得分:2)
错误信息非常简单:
joystick.c:14:1: error: number of arguments doesn’t match prototype
joystick.h:45:12: error: prototype declaration
open_joystick
的函数原型具有与实现不同的函数签名。在您的.h
文件中,您的原型具有签名:
int open_joystick(char *joystick_device);
但是在您的.c
文件中,您的函数定义具有签名:
int open_joystick()
签名不匹配 - 因此错误告诉您它们不匹配。
答案 1 :(得分:2)
看起来很简单 - 例如,这个声明:
struct wwvi_js_event {
int button[11];
int stick_x;
int stick_y;
};
此语句,wjse
类型为struct wwvi_js_event*
,
wjse->stick1_x = jse.value;
确实会合并产生这个错误:
joystick.c:57:16:错误:'struct wwvi_js_event'没有名字的成员 “stick1_x”
标头和.c
文件之间必定存在版本不匹配。