lash
lash

Reputation: 228

error: stdio.h: No such file or directory error during make

I'm trying to compile the following program in Ubuntu. But I keep getting the error: "stdio.h: No such file or directory" error.

#include <stdio.h>

int main(void)
{
  printf("Hello world");
}

My makefile is:

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

Upvotes: 9

Views: 24415

Answers (3)

MOHAMED
MOHAMED

Reputation: 43528

Your way of building your program is the way to build kernel module and not c program application. and stdio.h does not exist in the environment of the kernel development so that's why you get the error:

error: "stdio.h: No such file or directory" error 

1) If you want to build a linux application then your Makefile is wrong:

You should modify your Makefile

Use the following Makefile:

all: hello

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

clean:
    rm -r *.o hello

2) If you want to build a kernel module then your c code is wrong

  • YOU CAN NOT use stdio.h in the kernel space development. Itdoes not exist in the environment of the kernel development so that's why you get the error
  • YOU CAN NOT use main() in the kernel module C code
  • YOU CAN NOT use printf() in the kernel module C code

INSTEAD of using stdio.h, you have to use the following include

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */

INSTEAD of using int main() {, you have to use

int init_module(void) {

INSTEAD of using printf() use printk()

Use the following hello module instead of your hello code

/*  
 *  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");
}

Please refer to the following link for more detail about kernel module development

Upvotes: 25

Mike
Mike

Reputation: 49403

The problem is you can't use printf() and stdio.h inside the kernel, and you don't use a main() function either. You need printk() and module.h

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h>

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

    return 0;
}

You should also have a exit()/cleanup() type function for when it unloads.

void clean_module(void)
{
    printk(KERN_INFO "Cleaning and exiting\n");
}

Then load the functions for the module:

module_init(init_module);
module_exit(clean_module);

Upvotes: 5

Paul
Paul

Reputation: 1328

Your Makefile isn't correct, please take a look at one of the many tutorials, for example: http://mrbook.org/tutorials/make/

the simplest Makefile to build a standalone application should look something like that:

all: hello

hello: hello.o
    gcc hello.o -o hello

hello.o: hello.cpp
    gcc -c hello.cpp

clean:
    rm -rf *o hello

Hope it helps !

Upvotes: 0

Related Questions