# problem with my first kernel module,help



## alexzhengyi (Apr 24, 2009)

I'm new to freebsd,and I try my first hello world kernel module,I
got the following error on compilatiuon:

```
Warning: Object directory not changed from original /usr/home/alex/src/kernel/hello
cc -O2 -fno-strict-aliasing -pipe  -D_KERNEL -DKLD_MODULE -std=c99 -nostdinc   -I. -I@ -I@/contrib/altq -finline-limit=8000 --param inline-unit-growth=100 --param large-function-growth=1000 -fno-common  -mno-align-long-strings -mpreferred-stack-boundary=2  -mno-mmx -mno-3dnow -mno-sse -mno-sse2 -mno-sse3 -ffreestanding -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual  -Wundef -Wno-pointer-sign -fformat-extensions -c hello.c
ld  -d -warn-common -r -d -o .kld hello.o
:> export_syms
awk -f /sys/conf/kmod_syms.awk .kld  export_syms | xargs -J% objcopy % .kld
ld -Bshareable  -d -warn-common -o .ko hello
.kld
*** Error code 1

Stop in /usr/home/alex/src/kernel/hello.
```


```
And my source code:
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>

static int load(struct module *module, int cmd, void *arg)
{
    int error = 0;

    switch (cmd)
    {
        case MOD_LOAD:
        {
            uprintf("hello world!\n");
            break;
        }
        case MOD_UNLOAD:
        {
            uprintf("bye bye world\n");
            break;
        }
        default:
        {
            error = EOPNOTSUPP;
            break;
        }
    }

    return error;
}

static moduledata_t hello_mod = {
    "hello",
    load,
    NULL
};

DECLARE_MODULE(hello, hello_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
```


```
Makefile:
KMOD = hello
SRCS = hello.c

.include <bsd.kmod.mk>
```


----------



## graudeejs (Apr 24, 2009)

use code tags, it will make your source much more readable


----------



## alexzhengyi (Apr 24, 2009)

it'a not the fault of the code, I have already got the object file hello.o, but when generate hello.ko,it failed


----------



## alexzhengyi (Apr 24, 2009)

I have fixed it myself.


----------

