user3346727 user3346727 - 1 year ago 193
C++ Question

Include dlib in c++ project

I'm trying to get the dlib library working in my c++ project and don't know what to do with my Makefile and the #include<...> in the header file of my script. I have placed my header file and my script in the src directory. A symbolic link has been made to the dlib library, the link is in the include directory (see the folder structure below).

On the dlib website it says:

You should not add the dlib folder itself to your compiler's include path

Instead you should add the folder that contains the dlib folder to your include search path and then use include statements of the form #include . This will ensure that everything builds correctly.

The only thing I want is to include the dlib in my header file and make it compile so that I can use all the functions of dlib. Can you help me with this?

The folder structure is like this:

| |-main.cpp
| |-main.hpp
|-dlib (symbolic link)
| |-source.cpp
|- lots of header files

The makefile looks like this:

CC := g++ # This is the main compiler
# CC := clang --analyze # and comment out the linker last line for sanity
SRCDIR := src
LIBDIR := lib
BUILDDIR:= build
TARGET := bin/main

SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst$(SRCDIR)/%,$(BUILDDIR) /%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -g # -Wall
LIB := $(shell find $(LIBDIR) -type f -name *.$(SRCEXT))
INC :=

@echo " Linking..."
$(CC) $^ -o $(TARGET) $(LIB)

@mkdir -p $(BUILDDIR)
$(CC) $(CFLAGS) $(INC) -c -o $@ $<

@echo " Cleaning...";

.PHONY: clean

This is the main.cpp file:

#include "main.hpp"

int main(){
return 0;

And the header file: main.hpp, is empty since I really don't know what to do with it.

Answer Source

What the documentation is saying is that you must not configure your compiler so it finds the dlib includes by default, but do it in your project.

That makes sense, so when you distribute your makefile, it will work for other users without changing compiler configuration.

For your problem, just change

INC     :=


INC     := -Iinclude/dlib

You will be able to use dlib headers that you have added to your project like that:

#include <geometry.h>

(don't add the path, it is already in the compiler search path, driven by the -I option)

A bit out of scope of your question, but if you use functions from the dlib library (not only defines), you'll have a similar problem at the link phase (undefined symbols for called dlib symbols). That line could help but I think it is suspicious too:

LIB     := $(shell find $(LIBDIR) -type f -name *.$(SRCEXT))

It looks for source files in LIB (unless I'm mistaken, SRCEXT cannot contain library extensions)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download