【问题标题】:implicit declaration of function ‘ioctl’函数‘ioctl’的隐式声明
【发布时间】: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 &lt;sys/ioctl.h&gt;#include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt;
  • Simbols "" 没有帮助 :( 暗示隐式声明的相同错误

标签: c


【解决方案1】:

include 语句不正确,不需要完整路径。您可能需要:

#include <sys/fcntl.h> 
#include <sys/stat.h>
#include <sys/ioctl.h>      
#include <unistd.h>     
#include <stdio.h>
#include <stdlib.h>

【讨论】:

  • 哇,它对使用 gcc 编译很有帮助。你能帮我做Makefile吗?我使用完整路径,因为 Makefile 向我显示:iostl.c:60:24: fatal error: sys/fcntl.h: not found。现在 Makefile 再次显示这个错误。
  • 您正在使用一个用于编写 linux 内核模块的 Makefile,而您的程序似乎是一个普通的用户空间程序。从头开始并编写一个新的 Makefile - 您现在使用的 Makefile 是为完全不同的东西而设计的,您无法使用该 Makefile 使您的程序干净地编译
  • 谢谢,我明白了。
猜你喜欢
  • 2021-01-20
  • 1970-01-01
  • 2014-06-28
  • 2022-01-16
  • 2011-10-05
  • 2013-10-28
  • 2019-07-16
  • 2017-07-16
相关资源
最近更新 更多