简体   繁体   English

在C ++项目中包含dlib

[英]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. 我试图在我的c ++项目中使用dlib库,但不知道该如何处理我的Makefile和脚本头文件中的#include <...>。 I have placed my header file and my script in the src directory. 我已将头文件和脚本放置在src目录中。 A symbolic link has been made to the dlib library, the link is in the include directory (see the folder structure below). dlib库已建立符号链接,该链接位于include目录中(请参见下面的文件夹结构)。

On the dlib website it says: 在dlib网站上显示:

You should not add the dlib folder itself to your compiler's include path 您不应将dlib文件夹本身添加到编译器的包含路径中

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 . 相反,您应该将包含dlib文件夹的文件夹添加到包含搜索路径,然后使用格式为#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. 我唯一想做的就是将dlib包含在头文件中并进行编译,以便可以使用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: 这是main.cpp文件:

    #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. 头文件main.hpp是空的,因为我真的不知道该怎么做。

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. 该文档的意思是,您不得配置编译器,以便默认情况下它会找到dlib,但要在您的项目中进行。

That makes sense, so when you distribute your makefile, it will work for other users without changing compiler configuration. 这是有道理的,因此在分发makefile时,它将在不更改编译器配置的情况下对其他用户有效。

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: 您将可以像这样使用已添加到项目中的dlib头文件:

#include <geometry.h>

(don't add the path, it is already in the compiler search path, driven by the -I option) (不要添加路径,它已经在编译器搜索路径中,由-I选项驱动)

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). 您的问题有点超出范围,但是如果您使用dlib库中的函数(不仅定义),则在链接阶段会遇到类似的问题(未定义符号称为dlib符号)。 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) 它在LIB中查找源文件(除非我弄错了, SRCEXT不能包含库扩展名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM