错误:stdio.h:make期间没有此类文件或目录错误

las*_*ash 5 c ubuntu gnu-make

我正在尝试在Ubuntu中编译以下程序.但我一直收到错误:"stdio.h:没有这样的文件或目录"错误.

#include <stdio.h>

int main(void)
{
  printf("Hello world");
}
Run Code Online (Sandbox Code Playgroud)

我的makefile是:

obj-m += hello.o 
all:
    make -I/usr/include -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run Code Online (Sandbox Code Playgroud)

MOH*_*MED 18

构建程序的方法是构建内核模块而不是程序应用程序的方法.并且stdio.h在内核开发的环境中不存在,这就是你得到错误的原因:

error: "stdio.h: No such file or directory" error 
Run Code Online (Sandbox Code Playgroud)

1)如果你想构建一个linux应用程序,那么你的Makefile是错误的:

你应该修改你的Makefile

使用以下Makefile:

all: hello

test: test.c
    gcc -o hello hello.c

clean:
    rm -r *.o hello
Run Code Online (Sandbox Code Playgroud)

2)如果你想构建一个内核模块,那么你的c代码是错误的

  • 你不能使用stdio.h在内核空间的发展.它在内核开发的环境中不存在,这就是你得到错误的原因
  • 你不能使用main()的内核模块的C代码
  • 你不能使用printf()的内核模块的C代码

INSTEAD的使用stdio.h,你必须使用以下包括

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */
Run Code Online (Sandbox Code Playgroud)

使用INSTEADint main() {,你必须使用

int init_module(void) {
Run Code Online (Sandbox Code Playgroud)

INSTEAD的使用 printf()用途printk()

使用以下hello模块而不是hello代码

/*  
 *  hello-1.c - The simplest kernel module.
 */
#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");

    /* 
     * A non 0 return means init_module failed; module can't be loaded. 
     */
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}
Run Code Online (Sandbox Code Playgroud)

有关内核模块开发的更多详细信息,请参阅以下链接