【问题标题】:g++ complains about undefined reference even though library is included即使包含库,g ++也会抱怨未定义的引用
【发布时间】:2018-02-13 03:27:20
【问题描述】:

我有一个这样的示例文件,我们称之为dnn_mmod_face_detection_ex.cpp

#include <iostream>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>


using namespace std;
using namespace dlib;

// ----------------------------------------------------------------------------------------

template <long num_filters, typename SUBNET> using con5d = con<num_filters,5,5,2,2,SUBNET>;
template <long num_filters, typename SUBNET> using con5  = con<num_filters,5,5,1,1,SUBNET>;

template <typename SUBNET> using downsampler  = relu<affine<con5d<32, relu<affine<con5d<32, relu<affine<con5d<16,SUBNET>>>>>>>>>;
template <typename SUBNET> using rcon5  = relu<affine<con5<45,SUBNET>>>;

using net_type = loss_mmod<con<1,9,9,1,1,rcon5<rcon5<rcon5<downsampler<input_rgb_image_pyramid<pyramid_down<6>>>>>>>>;

// ----------------------------------------------------------------------------------------


int main(int argc, char** argv) try
{
    if (argc == 1)
    {
        cout << "Call this program like this:" << endl;
        cout << "./dnn_mmod_face_detection_ex mmod_human_face_detector.dat faces/*.jpg" << endl;
        cout << "\nYou can get the mmod_human_face_detector.dat file from:\n";
        cout << "http://dlib.net/files/mmod_human_face_detector.dat.bz2" << endl;
        return 0;
    }

    net_type net;
    deserialize(argv[1]) >> net;

    image_window win;
    for (int i = 2; i < argc; ++i)
    {
        matrix<rgb_pixel> img;
        load_image(img, argv[i]);

        // Upsampling the image will allow us to detect smaller faces but will cause the
        // program to use more RAM and run longer.
        while(img.size() < 1800*1800)
            pyramid_up(img);

        // Note that you can process a bunch of images in a std::vector at once and it runs
        // much faster, since this will form mini-batches of images and therefore get
        // better parallelism out of your GPU hardware.  However, all the images must be
        // the same size.  To avoid this requirement on images being the same size we
        // process them individually in this example.
        auto dets = net(img);
        win.clear_overlay();
        win.set_image(img);
        for (auto&& d : dets)
            win.add_overlay(d);

        cout << "Hit enter to process the next image." << endl;
        cin.get();
    }
}
catch(std::exception& e)
{
    cout << e.what() << endl;
}

我有一个像这样的 Makefile

CC=g++

CFLAGS=-c -Wall -std=c++11 -v
LDFLAGS=-L/usr/local/cuda/lib64 -ldlib -lcudnn -lpthread -ldl -lrt -lX11 -lcublas -lcudnn -lcurand -lcusolver -lstdc++ -lm -lgcc_s -lc -lxcb -lXau -lXdmcp
SOURCES=dnn_mmod_face_detection_ex.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=dnn_mmod_face_detection_ex
INCLUDE=
all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $(INCLUDE) $< -o $@

但我得到了对几乎每个 dlib 库的未定义引用。即

dnn_mmod_face_detection_ex.cpp:(.text+0x267): undefined reference to `dlib::image_window::image_window()'
dnn_mmod_face_detection_ex.cpp:(.text._ZNK4dlib8gpu_data4hostEv[_ZNK4dlib8gpu_data4hostEv]+0x14): undefined reference to `dlib::gpu_data::copy_to_host() const'
dnn_mmod_face_detection_ex.cpp:(.text._ZN4dlib16resizable_tensorC2Ev[_ZN4dlib16resizable_tensorC5Ev]+0x31): undefined reference to `dlib::cuda::tensor_descriptor::tensor_descriptor()'

我知道以下内容

  1. 同样的示例已编译并在 /dlib/build/test... 目录 (dlib-19.9) 中运行
  2. 那些LDFLAGS我通过发出命令ldd /dlib/build/test.../dnn_mmod_face_detection_ex获得

找出缺少哪些库的正确方法是什么?我尝试跟踪 dlib 提供的 Cmake 文件,但它比粒子加速器更复杂。

【问题讨论】:

  • 我在编译器标志中看不到任何包含目录。尝试包含头目录并构建。
  • 我只是假设正确找到了头文件,否则链接器将不知道该抱怨什么找不到。此外,g++ 正在寻找 dlib 文件夹所在的 /usr/local/include
  • @Sitesh 我可以确认添加dlib 以包含像/usr/local/include/dlib 这样的路径会破坏编译。 Dlib 希望你像这样调用函数#include&lt;dlib/string.h&gt;

标签: c++ makefile cmake linker


【解决方案1】:

这是一个简单的错误,将LDFLAGS 放错了位置。像这样放在末尾时它可以工作

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(OBJECTS) -o $@ $(LDFLAGS)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多