[英]implicit declaration of function ‘ioctl’
When I use gcc ioctl.c I have erors: 当我使用gcc ioctl.c时,会出现以下错误:
iostl.c:29:13: warning: implicit declaration of function 'ioctl' [-Wimplicit-function-declaration] iostl.c:29:13:警告:函数'ioctl'的隐式声明[-Wimplicit-function-declaration]
ret_val = ioctl (file_desc, IOCTL_SET_MSG, message); ret_val = ioctl (file_desc,IOCTL_SET_MSG,消息);
iostl.c:80:15: warning: implicit declaration of function 'open' [-Wimplicit-function-declaration] iostl.c:80:15:警告:函数'open'的隐式声明[-Wimplicit-function-declaration]
file_desc = open (DEVICE_FILE_NAME, 0); file_desc = 打开 (DEVICE_FILE_NAME,0);
What's wrong? 怎么了?
ioctl.c: ioctl.c:
#include </usr/include/linux/fcntl.h>
#include </usr/include/x86_64-linux-gnu/sys/stat.h>
#include </usr/include/linux/ioctl.h>
#include </usr/include/unistd.h>
#include </usr/include/stdio.h>
#include </usr/include/stdlib.h>
static int ioctl_set_msg(int file_desc, char *message);
static int ioctl_get_msg(int file_desc);
static int ioctl_get_nth_byte(int file_desc);
/* Functions for the ioctl calls */
int ioctl_set_msg(int file_desc, char *message)
{
int ret_val;
ret_val = ioctl(file_desc, IOCTL_SET_MSG, message);
if (ret_val < 0) {
printf ("ioctl_set_msg failed:%d\n", ret_val);
exit(-1);
}
}
int ioctl_get_msg(int file_desc)
{
int ret_val;
char message[100];
ret_val = ioctl(file_desc, IOCTL_GET_MSG, message);
if (ret_val < 0) {
printf ("ioctl_get_msg failed:%d\n", ret_val);
exit(-1);
}
printf("get_msg message:%s\n", message);
}
int ioctl_get_nth_byte(int file_desc)
{
int i;
char c;
printf("get_nth_byte message:");
i = 0;
while (c != 0) {
c = ioctl(file_desc, IOCTL_GET_NTH_BYTE, i++);
if (c < 0) {
printf("ioctl_get_nth_byte failed at the %d'th byte:\n", i);
exit(-1);
}
putchar(c);
}
putchar('\n');
}
int main()
{
int file_desc, ret_val;
char *msg = "Message passed by ioctl\n";
file_desc = open(DEVICE_FILE_NAME, 0);
if (file_desc < 0) {
printf ("Can't open device file: %s\n", DEVICE_FILE_NAME);
exit(-1);
}
ioctl_get_nth_byte(file_desc);
ioctl_get_msg(file_desc);
ioctl_set_msg(file_desc, msg);
close(file_desc);
}
Makefile: Makefile文件:
obj-m += iostl.o
all:
make cc -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make cc -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
test:test.c
cc -o test test.c
Command to making: make -C ./linux- uname -r
SUBDIRS=$PWD modules 制作命令:make -C ./linux- uname -r
SUBDIRS = $ PWD模块
The include statements are not correct and do not need a full path. include语句不正确,不需要完整路径。 You will probably need: 您可能需要:
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.