Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile cannot find include path

Tags:

c++

makefile

A Makefile in the subdirectory of my project doesn't see the include path ONLY when it is used from the main Makefile of my project. I don't have much experience with Makefiles and a lot of what I've been reading is pretty confusing. Here is the layout of my directory (with what I have so far since I just started the project):

main/
    Inventory/
        Item.h
        Item.cpp
        Makefile
    tools/
        include/
            json/
                json.h
        jsoncpp.cpp
        Makefile
    main.cpp
    Makefile

Here is the Makefile in the main directory:

INCLUDE = -IInventory/
CC = g++
DEP = tools/jsoncpp.o Inventory/Item.o

Main: main.o $(DEP)
    cd tools/ && make
    cd Inventory/ && make
    $(CC) -o Main main.o $(DEP) $(INCLUDE)

main.o main.cpp
    $(CC) -c main.cpp $(INCLUDE)

Here is the Makefile in the tools directory:

INCLUDE = -Iinclude/
CC = g++

jsoncpp.o: jsoncpp.cpp
    $(CC) -c jsoncpp.cpp $(INCLUDE)

When I call make from the tools/, it works just fine. But when I call make from the main directory I get this error:

g++    -c -o tools/jsoncpp.o tools/json.cpp
tools/jsoncpp.cpp:76:23: fatal error: json/json.h: No such file or directory
#include "json/json.h"
                      ^
compilation terminated.

Now I partially believe that it can't find the include directory for whatever reason, but the first line in that error is fairly odd to me because of that weird gap between g++ and -c. Since my project will soon get pretty big, how can I fix this?

like image 899
Cotant Avatar asked Sep 18 '25 13:09

Cotant


1 Answers

If it's in -I directive, it should be #include <json/json.h> otherwise #include "include/json/json.h"

EDIT: Include directory is taken from current directory, so in main/ you have to use -Itools/include

Solution: Implicit rules were used so correct variable CXXFLAGS+=$(INCLUDE) must be set for compilation. See: make manual

And the main problem is Main: main.o $(DEP) - files in DEP must exist already otherwise it'll use implicit rules. Later after that cd tools/ && make is done.

like image 194
KIIV Avatar answered Sep 20 '25 05:09

KIIV