在 C++ 项目中包含 dlib

Include dlib in c++ project

我试图让 dlib 库在我的 C++ 项目中工作,但不知道如何处理我的 Makefile 和脚本头文件中的#include<...>。我已将头文件和脚本放在 src 目录中。已对 dlib 库进行符号化 link,link 位于 include 目录中(请参阅下面的文件夹结构)。

dlib 网站上写着:

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.

我唯一想要的就是将dlib包含在我的头文件中并使其编译,以便我可以使用dlib的所有功能。你能帮我解决这个问题吗?

文件夹结构是这样的:

    projectDir
    |-Makefile
    |-src
    | |-main.cpp
    | |-main.hpp
    |-include
      |-dlib (symbolic link)
        |-all
        |  |-source.cpp
        |- lots of header files
        |-...

生成文件如下所示:

    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

这是 main.cpp 文件:

    #include "main.hpp"

    int main(){
        return 0;
    }

并且头文件:main.hpp 是空的,因为我真的不知道如何处理它。

文档中所说的是,您不得配置编译器以使其默认包含 dlib,而是在您的项目中进行配置。

这是有道理的,所以当您分发您的 makefile 时,它​​可以在不更改编译器配置的情况下为其他用户工作。

针对你的问题,改一下

INC     :=

来自

INC     := -Iinclude/dlib

您将能够像这样使用已添加到项目中的 dlib headers:

#include <geometry.h>

(不用加路径,已经在编译器搜索路径中,由-I选项驱动)

有点超出您的问题范围,但是如果您使用 dlib 库中的函数(不仅定义),您将在 link 阶段遇到类似的问题(未定义称为 dlib 符号的符号)。该行可能会有所帮助,但我认为它也很可疑:

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

它在 LIB 中查找源文件(除非我记错了,SRCEXT 不能包含库扩展名)