alteration
alteration

Reputation: 53

makefile in linux, unable to insert the module

I'm new in linux programming and I'm struggling with creation on makefile. I got 3 files: hello.c

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

int init_module(void) {
    asgard();
    return 0;
 }

void cleanup_module(void) {
     printk(KERN_INFO "Goodbye world 1.\n");
}

funcs.c

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

void asgard(void) {
    printk(KERN_INFO "Asgard balordo\n");
    return;
}

funcs.h

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

void asgard(void);

Then the makefile:

obj-m += hello.o
hello-objs := funcs.o

first:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

all: funcs.o hello.o
    gcc -o start funcs.o hello.o

funcs.o: funcs.c funcs.h
    gcc -C funcs.c

hello.o: hello.c funcs.h
    gcc -C hello.c

clean:
    rm -f *.o
    rm -f ./start
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

When I compile, everything is fine, when i call insmod ./hello.ko it's said that was impossible to insert the module. May you tell me where I'm wrong please?

Upvotes: 1

Views: 175

Answers (1)

Felipe Lavratti
Felipe Lavratti

Reputation: 2967

Well, you don't need to call GCC in your makefile to build a module, try this makefile:

obj-m += hello.o
hello-objs := funcs.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

Also, you could tell the module which function is the entry point and the exit point, and its good to declare module description, author and licensing. (Why here)

Try this:

static int __init enter_module(void)
{
    return 0;
}

static void __exit exit_module(void)
{
}

module_init(enter_module);
module_exit(exit_module);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("John <[email protected]>");
MODULE_DESCRIPTION("This is the module description.");

Kernel keywords __init and __exit are used to let the Kernel to do optimizations by removing functions from memory whenever possible.

While macros module_init and module_exit will register module entry and exit functions.

Upvotes: 1

Related Questions