How to Define Several Include Path in Makefile

How to define several include path in Makefile

You have to prepend every directory with -I:

INC=-I/usr/informix/incl/c++ -I/opt/informix/incl/public

Makefile: How to correctly include header file and its directory?

These lines in your makefile,

INC_DIR = ../StdCUtil
CFLAGS=-c -Wall -I$(INC_DIR)
DEPS = split.h

and this line in your .cpp file,

#include "StdCUtil/split.h"

are in conflict.

With your makefile in your source directory and with that -I option you should be using #include "split.h" in your source file, and your dependency should be ../StdCUtil/split.h.

Another option:

INC_DIR = ../StdCUtil
CFLAGS=-c -Wall -I$(INC_DIR)/.. # Ugly!
DEPS = $(INC_DIR)/split.h

With this your #include directive would remain as #include "StdCUtil/split.h".

Yet another option is to place your makefile in the parent directory:

root
|____Makefile
|
|___Core
| |____DBC.cpp
| |____Lock.cpp
| |____Trace.cpp
|
|___StdCUtil
|___split.h

With this layout it is common to put the object files (and possibly the executable) in a subdirectory that is parallel to your Core and StdCUtil directories. Object, for example. With this, your makefile becomes:

INC_DIR = StdCUtil
SRC_DIR = Core
OBJ_DIR = Object
CFLAGS = -c -Wall -I.
SRCS = $(SRC_DIR)/Lock.cpp $(SRC_DIR)/DBC.cpp $(SRC_DIR)/Trace.cpp
OBJS = $(OBJ_DIR)/Lock.o $(OBJ_DIR)/DBC.o $(OBJ_DIR)/Trace.o
# Note: The above will soon get unwieldy.
# The wildcard and patsubt commands will come to your rescue.

DEPS = $(INC_DIR)/split.h
# Note: The above will soon get unwieldy.
# You will soon want to use an automatic dependency generator.

all: $(OBJS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CC) $(CFLAGS) -c $< -o $@

$(OBJ_DIR)/Trace.o: $(DEPS)

C++ makefile with relative include path to parent

When using double-quotes to include a header file, as in

#include "dir2/child2/file2.h"

then the compiler will use the directory of the current file to search for the header file.

If you have it in your file1.h then the compiler will look for the header file [main_dir]/dir1/child1/dir2/child2/file2.h. Which isn't correct.

You can solve it by telling the compiler to add [main_dir] to the list of standard include search paths. This is done with the -I (upper-case i) option:

g++ -I [main_dir] main.cpp

Then when the compiler fails to find dir2/child2/file2.h in its first search, it will continue with the list of standard include search paths, and it should be found.

Why can't Makefile find include directories

Your include path -I/.. is invalid. You're trying to access the parent directory of the root directory, which cannot exist. Change your Makefile to use relative paths instead with -I..

This will access the parent directory as intended:

CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64 -Iinclude

test: test.o
g++ $(CFLAGS) -I.. -o test test.o # Change here

test.o: test.cpp
g++ $(CFLAGS) -I.. -c test.cpp # ...and here

Note the removed slashes.

EDIT: As commented by @Lightness, you should include non-system headers with "header.h" rather than <header.h>. Additionally, since you are trying to compile a C++ program, it is recommended to use g++ instead of gcc (I've updated this in the snippet above).

How to add multiple Headers files path in a Makefile?

This appears to be an attempt at a kbuild file,.

You should not be manually compiling the file yourself using your default rule. Instead, you should be running the kernel's makefile, and have it compile the driver based on obj-m and friends.

Your makefile would look like so:

ifneq ($(KERNELRELEASE),)

ccflags-y += -I some/other/dir
obj-m += uleds.o

else

# default to build against running kernel if KDIR not
# specified:
KDIR ?= /lib/modules/`uname -r`/build

default:
$(MAKE) -C $(KDIR) M=$$PWD

endif

If you call make from the driver's directory, it will in turn call make from your kernel directory, which will know everything about the kernel and will be able to properly build your module.

Notice that by default, the built-in kernel's clean target will remove all generated *.[oas] files, so no need for a special clean target. Also, by default, the kernel's makefile will include its own include directories, so you likely don't need to do anything special for that. In case you do need to include from somewhere else, you can add a -I directive to the ccflags-y as shown in the example.

See Linux Kernel Makefiles and Building External Modules for details.

How to include multiple header directories in g++ without many -I flags?

You could just add "-Iinclude" to g++ and include the headers as following

#include <foo/foo_header1.h>
#include <bar/bar_header2.h>

This also eliminates the need to have the foo/bar prefix in the header filename. After renaming, the above simplifies to follows

#include <foo/header1.h>
#include <bar/header2.h>


Related Topics



Leave a reply



Submit