ilansch
ilansch

Reputation: 4878

adding additional sources directory to c makefile

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

Upvotes: 0

Views: 1628

Answers (2)

blackghost
blackghost

Reputation: 1825

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...).

Upvotes: 2

Quentin Laill&#233;
Quentin Laill&#233;

Reputation: 125

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

Upvotes: 0

Related Questions