我两天前开始使用 Debian Linux。 安装gcc、g++编译器后, 我想使用命令(gcc file.c)运行 C 代码,但我在终端中发现了一个奇怪的错误。
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
操纵杆.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;
/* These are sensible on Logitech Dual Action Rumble and xbox360 controller. */
static int joystick_x_axis = 0;
static int joystick_y_axis = 1;
int open_joystick(char *joystick_device)
{
if (joystick_device == NULL)
joystick_device = JOYSTICK_DEVNAME;
joystick_fd = open(joystick_device, 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) {
if (jse.number == joystick_x_axis)
wjse->stick_x = jse.value;
if (jse.number == joystick_y_axis)
wjse->stick_y = jse.value;
} else if (jse.type == JS_EVENT_BUTTON) {
if (jse.number < 11) {
switch (jse.value) {
case 0:
case 1: wjse->button[jse.number] = jse.value;
break;
default:
break;
}
}
}
}
/* printf("%d\n", wjse->stick1_y); */
return 0;
}
void set_joystick_y_axis(int axis)
{
joystick_y_axis = axis;
}
void set_joystick_x_axis(int axis)
{
joystick_x_axis = axis;
}
#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);
}
}
}
操纵杆.h
#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
#endif
最佳答案
#if 0
的存在会排除 main
被编译。将其更改为#if 1
。由于您没有相应的 #endif
,因此您可以完全删除该行。
关于c - 在 Debian Linux 上使用 gcc 或 g++ 运行 C/C++ 代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29483773/