g++ header files in include subdirectory - c++

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.

Related

Setup makefile for 'path agnostic' inclusions

In my project files I just want to be able to say:
main.cpp:
#include <foo.h>
#include <bar.h>
When these headers files reside in separate
-Project
-include
-foo
foo.h
-bar
bar.h
-src
main.cpp
I've setup my make file to attempt to achieve this but I still get fatal error: foo.h: No such file or directory so I haven't been able to set it up correctly.
Makefile:
LIBS = ./include/foo ./include/bar
all:
g++ -o bin/myapp src/main.cpp $(LIBS) -std=c++11
Is LIBS correct? How can I achieve relative/agnostic include paths?
INCLUDES = -I./include/foo -I./include/bar
all:
g++ -o bin/myapp src/main.cpp $(INCLUDES) -std=c++11

Makefile cannot find include path

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.

Linking problems with jsoncpp

I'm trying to use the amalgamated version of jsoncpp in my own project. When using my makefile, it complains that it doesn't find json_tool.h:
input/jsoncpp.cpp:193:23: fatal error: json_tool.h: No such file or directory
#include "json_tool.h"
In my makefile I have:
jsoncpp.o: input/jsoncpp.cpp input/json/json.h
$(CXX) $(CXXFLAGS) -c input/jsoncpp.cpp $(LIBS)
with jsoncpp.cpp and json/json.h the ones created by the amalgamate.py script. What am I doing wrong?
You have not set up your include path properly
Add the following to your build command:
-I input/json/

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.

Why doesn't g++ use the -I option?

I'm trying to distribute a little software written on C++ on Linux that uses openGL and SFML. I'm trying to provide the sfml libraries and headers with my code in order to avoid the installation of sfml (people who will use it have no root access, but they will have all the openGL needed stuff installed).
My file hierarchy is as follows:
lib/ >> here goes all the sfml libraries (.so files)
Makefile >> later I show the code
src/ >> Here goes program and sfml sources
myfiles.h, myfiles.cpp >> all them compile and work allright with the sfml libraries installed at /usr/lib
SFML/ >> here goes all the sfml headers, has some subfolders
Here's my Makefile:
EJECUTABLE = app
MODULOS = src/main.o src/Handbapp.o src/Camera.o src/Light.o src/Scene.o src/Graphics.o src/Window.o src/Model.o src/Court.o src/Player.o src/Primitives.o src/Path.o
CC = g++
LIBDIR = ./lib
INCDIR = ./src
LIBS = -lsfml-window -lsfml-system -lGLU
LDFLAGS = -L$(LIBDIR) -I$(INCDIR)
CFLAGS = -v -Wl,-rpath,$(LIBDIR)
$(EJECUTABLE): clean $(MODULOS)
$(CC) $(CFLAGS) -o $(EJECUTABLE) $(LDFLAGS) $(MODULOS) $(LIBS)
rm -f $(MODULOS)
clean:
rm -f $(MODULOS) $(EJECUTABLE)
When I run make in a PC (Ubuntu 11.10) with sfml installed in /usr/lib it all goes well, if I do in one that doesn't have it installed it says:
...
g++ -c -o src/main.o src/main.cpp
In file included from src/main.h:18:0,
from src/main.cpp:10:
src/Handbapp.h:17:44: fatal error: SFML/Window.hpp: File or directory doesn't exist
Compilation finished.
make: *** [src/main.o] Error 1
Here's a piece of code showing the include on Handbapp.h:
...
#ifndef HANDBAPP_H
#define HANDBAPP_H
// Espacio de nombres
using namespace std;
// Librerias
#include <GL/gl.h> // OpenGL
#include <GL/glu.h> // Utilidades OpenGL
#include <SFML/Window.hpp> // Ventanas SFML <- LINE 17
#include <SFML/System.hpp> // SFML
I've tried making #include "whatever/Window.hpp",changing src/SFML folder name for whatever and not using the -I option on the linker, but src/SFML/Window.hpp (and other sfml headers) have include lines like that #include < SFML/Window/Whatever.hpp >, so I need them to search at the path I specify.
Am I missing something? I guess it's an error with my Makefile, but I don't have so much experience with it...
Thanks in advance!
You need to put -I$(INCDIR) into CPPFLAGS, not LDFLAGS. Then it will be picked up by the built-in rule for compilation of individual object files.
You should also rename CFLAGS to CXXFLAGS and CC to CXX. CC and CFLAGS are for C source files, not C++ source files.
You should not have $(EJECUTABLE) depend on clean, and you should not execute rm -f $(MODULOS) after the link command. Those things defeat the purpose of a Makefile, which is to recompile only what is necessary, not the whole program every single time.
The problem isn't in the compiler but in your Makefile: you want to set up the include directory path in the appropriate flags (I typically use CPPFLAGS but I typically also have my own rules which explicitly reference the flags I'm using). The LDFLAGS are definitely only passed to the linking stage of the build.