# How to compile module with source in subdir



## Nevar512 (Feb 14, 2015)

Hi

I have sample hello world module in subdirectory:

```
./module-1.1/module.c
./Makefile
```
and

```
$ cat Makefile
KMOD=m-1.1-hello
SRCS=module-1.1/module.c
.include <bsd.kmod.mk>
```
When I try to compile it from the current directory then I get:

```
$ make
cc -O2 -pipe  -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc  -I. -I@ -I@/contrib/altq -fno-common  -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer  -mno-aes -mno-avx -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float  -fno-asynchronous-unwind-tables -ffreestanding -fstack-protector -std=iso9899:1999 -Qunused-arguments  -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual  -Wundef -Wno-pointer-sign -fformat-extensions  -Wmissing-include-dirs -fdiagnostics-show-option  -Wno-error-tautological-compare -Wno-error-empty-body  -Wno-error-parentheses-equality -Wno-error-unused-function  -c module-1.1/module.c
ld  -d -warn-common -r -d -o m-1.1-hello.ko module-1.1/module.o
ld: module-1.1/module.o: No such file: No such file or directory
*** Error code 1
```
This is strange because linker tries to link file in module-1.1 but compiler created them in the current directory:

```
$ ls
@                     Makefile
machine            module-1.1
module.o           x86
```

I am doing something wrong or compilation from subdirectory is not supported ?


----------



## trev (Feb 25, 2015)

I do what FreeBSD does by default:

1. Put your module.c in /usr/src/sys/dev/module-1.1/

2. Put your Makefile in /usr/src/sys/modules/module-1.1/

3. Add `.PATH: ${.CURDIR}/../../dev/module-1.1` to the top of your Makefile

and it just works


----------

