我想用c程序从用户空间调用内核模块。编译时出现了这个错误

header.h:13:38: error: expected expression before ‘char’
 #define IOCTL_CMD _IORW(MAGIC_NO, 0, char *)

根据定义,我提出了正确的论点:driver.ko
主c
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include "header.h"

int main()
{
    int fd;
    char * msg = "5";
    fd = open(DEVICE_PATH, O_RDWR);
    ioctl(fd, IOCTL_CMD, msg);
    printf("ioctl executed\n");
    close(fd);
    return 0;
}

标题.h
#include <linux/ioctl.h>
#include <linux/kdev_t.h> /* for MKDEV */

#define DEVICE_NAME "driver"
#define DEVICE_PATH "/dev/driver"
#define WRITE 0
static int major_no;

#define MAGIC_NO '4'
    /*
     * Set the message of the device driver
     */
#define IOCTL_CMD _IORW(MAGIC_NO, 0, char *)

最佳答案

_IORW似乎不存在于Linux标题中,而是尝试使用_IOWR。我也不认为你使用的char *是正确的。这意味着ioctl的最后一个参数是char *变量的地址,而不是字符串。

关于c - ioctl调用程序编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25185767/

10-11 23:21
查看更多