0

sorry if this is noob question.

In my c function, I use a macro defined in btrfs_inode.h file.
When I include the file directly to path:

#include "/data/kernel/linux-4.1.21-x86_64/fs/btrfs/btrfs_inode.h"

the project compiles with no errors, I dont want to use that direct path, I download the package kernel-source that contains this header file.
The location of the header file after installing the package is at: /usr/src/linux/fs/btrfs/

So I change the #include to :

#include "btrfs_inode.h"

and i wish to add "/usr/src/linux/fs/btrfs/" as a location that it will search for "btrfs_inode.h" and get: "/usr/src/linux/fs/btrfs/btrfs_inode.h"

I get error:
/bin/sh: 1: /usr/src/linux/fs/btrfs/: Permission denied

I am running make as root.

Makefile:

all:: user

obj-m += my-driver.o


# Make arguments
PWD := $(shell pwd)
INCLUDE := -I/usr/include/asm/mach-default/
KDIR    := /lib/modules/$(KERNEL_HEADERS)/build;/usr/src/linux/fs/btrfs/


# Add flags to auto build
EXTRA_CFLAGS    +=-D__Linux -std=gnu99

# extra warning flags
ccflags-y := -Wall -Wextra #-pedantic
# disable some warning flags
ccflags-y += -Wno-unused-parameter
# make all warnings into errors
ccflags-y += -Werror
# increase verbosity
KBUILD_VERBOSE := 1

all::
$(MAKE) -C $(KDIR) $(INCLUDE) SUBDIRS=$(PWD) modules
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
ilansch
  • 4,784
  • 7
  • 47
  • 96

2 Answers2

1

So first off, avoid making as root when possible. Next, you added your directory to KDIR, not to INCLUDE (and then you pass KDIR to the -C argument of make, so you would have a line that looks like:

make -C /lib/modules/$(KERNEL_HEADERS)/build;/usr/src/linux/fs/btrfs/ ...

Notice the semicolon, which bash will interperet as the end of a command, and beginning of the next command. So it tries to run make, and then tries to run /usr/src/linux/fs/btrfs/, and gives you your warning. What you should have is something like:

# Make arguments
PWD := $(shell pwd)
INCLUDE := -I/usr/include/asm/mach-default/
INCLUDE += -I/usr/src/linux/fs/btrfs/
KDIR    := /lib/modules/$(KERNEL_HEADERS)/build

(you want a -I in front of the path to tell make to search for include files in that directory).

EDIT

You are also not passing the -I to your $(CC) or $(CXX) commands. To do this, you have a couple of options, though I'll suggest the least error prone one: First of all, you have to pass the flags to the sub make. To do this, first add the line:

export INCLUDE

to your main makefile. Your submake now has access to the variable $(INCLUDE). From there, if you have an explicit rule to compile the CC files, you can add $(INCLUDE) to the compile command. Something like

%.o: %.c
     $(CC) $(CFLAGS) $(INCLUDE) -o $@ $<

or, if you are using the built-in implicit rules, simply add $(INCLUDE) to CPP_FLAGS:

CPP_FLAGS += $(INCLUDE)

(note, CPP_FLAGS are used by default for both c and c++ compilation).

Finally, do not pass $(INCLUDE) to your make command. If you do, it tells make to look look for sub-makefiles in those directories (not gcc...).

blackghost
  • 1,730
  • 11
  • 24
  • I have added INCLUDE += -I/usr/src/linux/fs/btrfs/ and i get: "make -C /lib/modules/4.1.21.x86_64.1/build -I/usr/include/asm/mach-default/ -I/usr/src/linux/fs/btrfs SUBDIRS=/home/ilan/unix/v6.3/Filer/Linux modules", i included the file: #include - i get compilation error ctree.h: No such file or directory, it is in the directory... If i dont use the new -I include.. and just do: #include it will work.. what is wrong ? – ilansch Mar 21 '17 at 06:32
  • Ahh, I overlooked the fact that you were passing `$(INCLUDE)` to `$(MAKE)` rather than to `$(CC)`. Editing my answer to include this as well. – blackghost Mar 22 '17 at 13:50
0

From what I could understand via this question, you can add multiple -I flags to your Makefile.

Community
  • 1
  • 1
Quentin Laillé
  • 125
  • 1
  • 12