简体   繁体   中英

implicit declaration of function ‘ioctl’

When I use gcc ioctl.c I have erors:

iostl.c:29:13: warning: implicit declaration of function 'ioctl' [-Wimplicit-function-declaration]
ret_val = ioctl (file_desc, IOCTL_SET_MSG, message);

iostl.c:80:15: warning: implicit declaration of function 'open' [-Wimplicit-function-declaration]
file_desc = open (DEVICE_FILE_NAME, 0);
What's wrong?

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

Command to making: make -C ./linux- uname -r SUBDIRS=$PWD modules

The include statements are not correct and do not need a full path. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM