Makefile cannot find include path - c++

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?

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.

#include <json/json.h> <- preprocessor will search in all includes paths
see gcc Include Syntax :
#include <file> This variant is used for system header files. It searches for a file named file in a standard list of system
directories. You can prepend directories to this list with the -I
option (see Invocation).
#include "file" This variant is used for header files of your own program. It searches for a file named file first in the directory
containing the current file, then in the quote directories and then
the same directories used for . You can prepend directories to
the list of quote directories with the -iquote option.

Related

Headerfiles used by initFile cant be found by make

I'm tying to write a makefile, that should compile (and link) my program. For my program I have to use a bigger Libray with many .h and .cpp files, which comes in a specified filestructure(many other subdirectories)(I'm not allowed to change that stucture)
My main.cpp is included in the first directory which also includes the makefile. This.cpp file includes headers out of the bigger library. And here comes the problem, when i try to "make" the terminal says:" fatal error: .. .h: File or Directory not found" #include ".. .h"
BTW: I'm using Ubuntu 18.04.1 with Gcc and gnu-make
Sooo... I tried several things the last 5 days.
I tried to do it with a dependfile like this:
SRC = datei1.c datei2.c datei3.c datei4.c datei5.c
CC = /usr/bin/gcc
DEPENDFILE = .depend
dep: $(SRC)
$(CC) -MM $(SRC) > $(DEPENDFILE)
-include $(DEPENDFILE)
(Not sure if i made it the right way)
I was thinking of just including every single header file with include, but that would be waaay to much!
I guess the most powerful and useful thing till now was this URL:
Makefile: How to correctly include header file and its directory?.
That nearly described perfectly my problem, but it was just useful to include one single header file and not the whole library.
I guess it could be helpful to know how to correctly include a library. (Maybe try some ways over the PATH?)
Folder Sructure:
myproj
|
|____Makefile
|____main.cpp
|____init.cpp
|____end.cpp
|____init.h
|____end.h
|____Dependencies
|____biggerlib
|____src
|____include
|____biggerLib1
|____biggerLib2
|____biggerLib
|____biggerLibrary.h
|____Lib2.h
|____Lib3.h
|____AnotherDirWithFiles1
|____AnotherDirWithFiles2
|____AnotherDirWithFiles3
|____etc.
#include in file:
#include "biggerLib/biggerLibrary.h"
I really do'nt know what to do anymore!
#Compiler directory
CC = gcc
#directories
SDCC = /home/myname/myproj/Dependencies/biggerlib/src
SDCH = /home/myname/myproj/Dependencies/biggerlib/include/biggerLib # normally ere are a few files and more directories!
#Dependenfile
#DEPENDFILE = .depend
#dep: $(SDCC)
# $(CPP) -MM $(SDCC) > $(DEPENDFILE)
#-include $(DEPENDFILE)
#C-Flags for object-compiling
CFLAGS = -c -I$(SDCH)/.. #ugly!
#Deps including every single one? too much work!
#DEPS = $(SDCH)/biggerlibfile.h
#Libs for Compiler
#LIBS = -lSDCH
#Loading object list
include objects.mk
#Main-target (linking)
$(NAME) : $(OBJECTS)
$(CC) -o $(NAME) #$+ $(LIBS)
#Object-targets
%.o : %.cpp #$(DEPENDFILE) #$(DEPS)
$(CC) -o $# $< $(CFLAGS)
Edit:
The single header file is working, but how do i include all the other header files in the even deeper directories? do I really need to include them all the same way? I need to include every Header behind "include".
I hope u guys understand my problem!:)
Looking forward to your suggestions and tips!

Makefile for different .cpp files in different folders

I have the following folder structure:
Project project:
folder1
sourceFile1_1.cpp
sourceFile1_2.cpp
folder2
sourceFile2.cpp
folder3
sourceFile3.cpp
main.cpp
makefile
And I'd like to create a makefile which builds the project. The source files can have dependencies among them, but there is not any cyclic dependency.
This is the makefile I have right now:
all: project.exe
clean:
rm main.o project.exe
project.exe: main.o
g++ -g -o main main.o
main.o:
g++ -c -g main.cpp
But when I run it, I get Cannot open include file: 'whatever....h': No such file or directory
Does someone know what I'm doing wrong?
cannot open include file, means that the compiler was not able to find the file. The compiler uses the include path to search for include files. You can look at the documentation here for more information about how you can set this include path.

g++ header files in include subdirectory

I'm including the freetype2 library by calling the freetype2/ft2build.h header file.
#include <freetype2/ft2build.h>
The header file itself calls another header file in a subdirectory.
#include <config/ftheader.h>
Which causes the 'make' to fail.
/usr/include/freetype2/ft2build.h:37:29: fatal error: config/ftheader.h: No such file or directory
#include <config/ftheader.h>
The 'config' subdirectory is within the 'freetype2', but the compiler is clearly looking for it in the /usr/include directory.
I cannot find a solution to this, but I can't possibly be the only one it affects. What am I missing?
I've tried adding INCLUDE=-I/usr/include/freetype2/config to the makefile but this doesn't work.
Makefile is as follows
INCLUDE=-I/usr/include/freetype2
CC=g++
LDLIBS=-lglut -lGLEW -lGL
all: main
clean:
rm -f *.o main
.PHONY: all clean
I think you need to replace
#include <freetype2/ft2build.h>
with
#include <ft2build.h>
and use
INCLUDE=-I/usr/include/freetype2
in makefile
Changed my Mkefile.
CPPFLAGS=-I/usr/include/freetype2 -I/usr/include/freetype2/config
CC=g++
LDLIBS=-lglut -lGLEW -lGL
all: main
clean:
rm -f *.o main
.PHONY: all clean
from INCLUDE to CPPFLAGS.

Unexpected behavior of g++ -MG flag

I have main.cpp file for which I want to generate dependency file main.d (to be included in Makefile).
I'm calling g++ -MM -MF src/main.d -MP -MT src/main.o src/main.cpp. It works fine unless I have some source generator. I found -MG flag for this, but it doesn't work as I expected it to work.
For example: main.cpp includes module/mod.h (#include "module/mod.h") and mod.h includes generator/gen.h (#include "generator/gen.h"), gen.h is generated. There is following structure:
-Makefile
-src
--main.cpp
--module
---mod.h
---generator
----gen.h
In Makefile I have a rule to generate src/module/generator/gen.h.
Generated by g++ dependencies for existing files are correct:
src/main.o: src/module/mod.h
But for non-existing file gen.h dependency is just generator/gen.h.
If I generate this file before generating dependencies it is correct src/module/generator/gen.h.
Is there any other option for g++ to correctly generate dependencies for non-existing files?
main.d generated when gen.h doesn't exist:
src/main.o: src/module/mod.h generator/gen.h
Correct main.d generated when gen.h exist:
src/main.o: src/module/mod.h src/module/generator/gen.h
As stated in man gcc
The dependency filename is taken directly from the "#include" directive without prepending path
actually there's no way for gcc to know the real path.
I'd recommend you to fix the include statement to keep include path in sync with make path, i.e. white in mod.h
#include <module/generator/gen.h>
put Makefile to src and add proper -I flag to gcc, e.g.
SRCDIR := $(shell pwd)
CXXFLAGS += -I$(SRCDIR)
it is a good practice to always start all non-local includes from some 'source root'

Compiling C++, organising include files

I'm trying to modify and compile uvccapture on the Raspberry Pi. I got the source from here (it's just a few files).
(I think) the only external files it needs are those of jpeglib which I downloaded from here.
When compiling, where do I put the jpeglib source files? UVCCapture has the following line:
#include <jpeglib.h>
Does that mean I should put the jpeglib source files in the same directory as the UVCCapture source files? That seems messy. How can I set up the compiler (modify the Makefile?), and where should I put the jpeglib files so that I don't need to change the uvccapture include file lines?
And a side question, how come it only includes the .h file and not the .c file? (I'm pretty new to C/C++)
Here is the Makefile:
CC=gcc
CPP=g++
APP_BINARY=uvccapture
VERSION = 0.4
PREFIX=/usr/local/bin
WARNINGS = -Wall
CFLAGS = -std=gnu99 -O2 -DLINUX -DVERSION=\"$(VERSION)\" $(WARNINGS)
CPPFLAGS = $(CFLAGS)
OBJECTS= uvccapture.o v4l2uvc.o
all: uvccapture
clean:
#echo "Cleaning up directory."
rm -f *.a *.o $(APP_BINARY) core *~ log errlog
install:
install $(APP_BINARY) $(PREFIX)
# Applications:
uvccapture: $(OBJECTS)
$(CC) $(OBJECTS) $(XPM_LIB) $(MATH_LIB) -ljpeg -o $(APP_BINARY)
Thanks
The source file (uvccapture.c) doesn't care where the header file (jpeglib.h) is -- at least it shouldn't. The compiler must be told where to look for header files; traditionally, the header files go in some directory like inc_files/, and the compiler is invoked with a command like
gcc -blah -blah -blah -Iinc_files -c -o uvccapture.o uvccapture.c
If you use Make, then Make should execute a command like that. So either edit the makefile, or put the header files in the current directory.
The sane way to use #include in C/C++ is to have source files and header files include header files. That is, in foo.c there will be a couple of lines like:
#include <bar>
#include "baz.h"
and in baz.h there might be a few lines like:
#include <vector>
#include "qux.h"
You almost never see #include foo.c, because it's almost never a good idea.