在c ++项目中包含dlib [英] Include dlib in c++ project

查看:369
本文介绍了在c ++项目中包含dlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让dlib库在我的c ++项目中运行,并且不知道如何处理我的Makefile以及我脚本的头文件中的#include< ...>。我已将头文件和脚本放在src目录中。已经对dlib库进行了符号链接,链接位于include目录中(参见下面的文件夹结构)。



在dlib网站上它说:


您不应将dlib文件夹本身添加到编译器的包含路径



相反,您应该将包含dlib文件夹的文件夹添加到包含搜索路径,然后使用#include形式的include语句。这将确保一切正确构建。


我唯一想要的是将dlib包含在我的头文件中并使其编译以便我可以使用所有的功能DLIB。你可以帮帮我吗?



文件夹结构如下:

  projectDir 
| -Makefile
| -src
| | -main.cpp
| | -main.hpp
| -include
| -dlib(符号链接)
| -all
| | -source.cpp
| - 许多头文件
| -...

makefile如下所示:

  CC:= g ++#这是主编译器
#CC: = clang --analyze#并注释掉链接器的最后一行为了理智
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链接......
$(CC)$ ^ -o $(TARGET)$(LIB)

$(BUILDDIR)/%。o:$(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(BUILDDIR)
$(CC)$(CFLAGS)$(INC)-c -o $ @ $& LT;

清洁:
@echo清洁......;
$(RM)-r $(BUILDDIR)$(TARGET)

.PHONY:clean

这是main.cpp文件:

  #includemain.hpp

int main(){
return 0;
}

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

解决方案

文档说的是你不能配置你的编译器以便它找到dlib默认包含,但在你的项目中执行。



这是有道理的,因此当你分发你的makefile时,它将适用于其他用户而不改变编译器配置。 / p>

对于您的问题,只需更改

  INC:= 

by

  INC:= -Iinclude / dlib 

您将能够使用已添加到的dlib标头你的项目是这样的:

  #include< geometry.h> 

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



有点超出问题的范围,但是如果你使用<$ c中的函数$ c> dlib 库(不仅定义),在链接阶段你会遇到类似的问题(被调用的dlib符号的未定义符号)。这条线可能会有所帮助,但我认为它也是可疑的:

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

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


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.

解决方案

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)

这篇关于在c ++项目中包含dlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆