-1

I know there are many such questions asked on stackoverflow but I can't find the solution to ny problem. So I have written a file in c which refers to .h files in /usr/include and here is my make file

CC= gcc
obj-m+=ghost.o
INC_DIR= /usr/include/
CFLAGS= -B$(INC_DIR)

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

There is a file in /usr/include . I can't find a way to add this directory in my makefile. According to gcc man page we can specify extern directory using -B directory_name

Divyanshu
  • 3
  • 6
  • It looks like you're trying to build a kernel module. You can't use standard C runtime content (like the content from /usr/include) in a kernel module. You have to use the kernel's runtime functions only. – MadScientist Oct 20 '19 at 12:38
  • So how do I include the .h files needed form /usr/include? – Divyanshu Oct 22 '19 at 14:04
  • If you're indeed building a Linux kernel module (you should say this outright in your question, always, because it makes a huge difference in the answer) then you should _never_ use headers (or anything else) from `/usr/include`. That directory contains _user-space_ headers. It's not correct to include them in kernel modules. The kernel has its own runtime library with its own headers and you should use those instead. – MadScientist Oct 22 '19 at 14:26
  • Okay! But I need to include dirent structure present in dirent.h file in my kernel module! How should I do this? – Divyanshu Oct 26 '19 at 13:01
  • You'll have to find the equivalent facility in the kernel's header files. Why do you need dirent? You can't call standard readdir etc. from inside the kernel. In any event this is not the right place for this discussion. You should do some googling on what you're trying to accomplish, being sure to look for results that specifically pertain to writing kernel modules not user-space programs. Once you have specific questions you can ask on SO, but use the correct tags and text so people understand you're talking about kernel modules. – MadScientist Oct 28 '19 at 12:43

1 Answers1

0

use -I [1].

Saying that, /usr/include should be in your default search paths already. Use this answer to find out.

Well-behaved makefiles normally let you override or append to CPPFLAGS. Which is where you should specify additional -I options for the gcc preprocessor:

CPPFLAGS='-I/usr/include' make

[1]:

from man gcc

Options for Directory Search
   These options specify directories to search for header files, for
   libraries and for parts of the compiler:

   -I dir
   -iquote dir
   -isystem dir
   -idirafter dir
       Add the directory dir to the list of directories to be searched
       for header files during preprocessing.  If dir begins with = or
       $SYSROOT, then the = or $SYSROOT is replaced by the sysroot
       prefix; see --sysroot and -isysroot.

       Directories specified with -iquote apply only to the quote form
       of the directive, "#include "file"".  Directories specified with
       -I, -isystem, or -idirafter apply to lookup for both the
       "#include "file"" and "#include <file>" directives.

       You can specify any number or combination of these options on the
       command line to search for header files in several directories.
       The lookup order is as follows:

       1.  For the quote form of the include directive, the directory of
           the current file is searched first.

       2.  For the quote form of the include directive, the directories
           specified by -iquote options are searched in left-to-right
           order, as they appear on the command line.

       3.  Directories specified with -I options are scanned in left-to-
           right order.

       4.  Directories specified with -isystem options are scanned in
           left-to-right order.

       5.  Standard system directories are scanned.

       6.  Directories specified with -idirafter options are scanned in
           left-to-right order.

       You can use -I to override a system header file, substituting
       your own version, since these directories are searched before the
       standard system header file directories.  However, you should not
       use this option to add directories that contain vendor-supplied
       system header files; use -isystem for that.

       The -isystem and -idirafter options also mark the directory as a
       system directory, so that it gets the same special treatment that
       is applied to the standard system directories.

       If a standard system include directory, or a directory specified
       with -isystem, is also specified with -I, the -I option is
       ignored.  The directory is still searched but as a system
       directory at its normal position in the system include chain.
       This is to ensure that GCC's procedure to fix buggy system
       headers and the ordering for the "#include_next" directive are
       not inadvertently changed.  If you really need to change the
       search order for system directories, use the -nostdinc and/or
       -isystem options.
Ente
  • 2,301
  • 1
  • 16
  • 34
  • Well i have already tried doing it with -I but still make won't show any difference. – Divyanshu Oct 20 '19 at 11:38
  • what exactly have you tried? And what did not work? Please try to describe this in your questions. this is really helpful information. – Ente Oct 20 '19 at 11:45
  • ```CC= gcc obj-m+=ghost.o CPPFLAGS=-I/usr/include all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean``` This is my makefile ! Wont work – Divyanshu Oct 22 '19 at 14:03
  • have you tried exporting those variables? [This might help](https://www.gnu.org/software/make/manual/html_node/Variables_002fRecursion.html) – Ente Oct 22 '19 at 14:08