【发布时间】:2017-11-26 15:07:13
【问题描述】:
当我使用 gcc ioctl.c 时出现错误:
iostl.c:29:13: 警告:函数 'ioctl' [-Wimplicit-function-declaration] 的隐式声明
ret_val = ioctl(file_desc, IOCTL_SET_MSG, 消息);
iostl.c:80:15: 警告:函数‘open’的隐式声明 [-Wimplicit-function-declaration]
file_desc = open(DEVICE_FILE_NAME, 0);
怎么了?
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);
}
生成文件:
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
制作命令:make -C ./linux-uname -r SUBDIRS=$PWD modules
【问题讨论】:
-
如果您从
#includes 中删除/usr/include/linux/,警告会消失吗? -
为什么
-
如果我删除 /usr/include/linux/ 那么我有:致命错误:ioctl.h not found
-
包括
#include <sys/ioctl.h>和#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -
Simbols "" 没有帮助 :( 暗示隐式声明的相同错误
标签: c