[英]implicit declaration of function ‘ioctl’
當我使用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 = 打開 (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);
}
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
制作命令:make -C ./linux- uname -r
SUBDIRS = $ PWD模塊
include語句不正確,不需要完整路徑。 您可能需要:
#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.