user3346727 user3346727 - 3 months ago 43
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:

projectDir
|-Makefile
|-src
| |-main.cpp
| |-main.hpp
|-include
|-dlib (symbolic link)
|-all
| |-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 :=

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

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(BUILDDIR)
$(CC) $(CFLAGS) $(INC) -c -o $@ $<

clean:
@echo " Cleaning...";
$(RM) -r $(BUILDDIR) $(TARGET)

.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

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     :=

by

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)

Comments