为什么我无法链接这个库?

时间:2016-05-25 06:11:32

标签: c++ linux g++

我尝试从FLANN库中编译此flann_example.cpp

#include <flann/flann.hpp>
#include <flann/io/hdf5.h>

#include <stdio.h>

using namespace flann;

int main(int argc, char** argv)
{
    int nn = 3;

    Matrix<float> dataset;
    Matrix<float> query;
    load_from_file(dataset, "dataset.hdf5","dataset");
    load_from_file(query, "dataset.hdf5","query");

    Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
    Matrix<float> dists(new float[query.rows*nn], query.rows, nn);

    // construct an randomized kd-tree index using 4 kd-trees
    Index<L2<float> > index(dataset, flann::KDTreeIndexParams(4));
    index.buildIndex();                                                                                               

    // do a knn search, using 128 checks
    index.knnSearch(query, indices, dists, nn, flann::SearchParams(128));

    flann::save_to_file(indices,"result.hdf5","result");

    delete[] dataset.ptr();
    delete[] query.ptr();
    delete[] indices.ptr();
    delete[] dists.ptr();

    return 0;
}

这就是我尝试编译它的方式:

g++ flann_example.cpp -I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial/lib -L/usr/lib/x86_64-linux-gnu -lhdf5

但是我收到了这个错误:

/tmp/ccpHAtGt.o: In function `flann::serialization::SaveArchive::initBlock()':
flann_example.cpp:(.text._ZN5flann13serialization11SaveArchive9initBlockEv[_ZN5flann13serialization11SaveArchive9initBlockEv]+0xb9): undefined reference to `LZ4_resetStreamHC'
/tmp/ccpHAtGt.o: In function `flann::serialization::SaveArchive::flushBlock()':
flann_example.cpp:(.text._ZN5flann13serialization11SaveArchive10flushBlockEv[_ZN5flann13serialization11SaveArchive10flushBlockEv]+0xc5): undefined reference to `LZ4_compress_HC_continue'
flann_example.cpp:(.text._ZN5flann13serialization11SaveArchive10flushBlockEv[_ZN5flann13serialization11SaveArchive10flushBlockEv]+0x18c): undefined reference to `LZ4_compress_HC_continue'
/tmp/ccpHAtGt.o: In function `flann::serialization::LoadArchive::decompressAndLoadV10(_IO_FILE*)':
flann_example.cpp:(.text._ZN5flann13serialization11LoadArchive20decompressAndLoadV10EP8_IO_FILE[_ZN5flann13serialization11LoadArchive20decompressAndLoadV10EP8_IO_FILE]+0x225): undefined reference to `LZ4_decompress_safe'
/tmp/ccpHAtGt.o: In function `flann::serialization::LoadArchive::initBlock(_IO_FILE*)':
flann_example.cpp:(.text._ZN5flann13serialization11LoadArchive9initBlockEP8_IO_FILE[_ZN5flann13serialization11LoadArchive9initBlockEP8_IO_FILE]+0x1f7): undefined reference to `LZ4_setStreamDecode'
/tmp/ccpHAtGt.o: In function `flann::serialization::LoadArchive::loadBlock(char*, unsigned long, _IO_FILE*)':
flann_example.cpp:(.text._ZN5flann13serialization11LoadArchive9loadBlockEPcmP8_IO_FILE[_ZN5flann13serialization11LoadArchive9loadBlockEPcmP8_IO_FILE]+0xcc): undefined reference to `LZ4_decompress_safe_continue'
collect2: error: ld returned 1 exit status

然后,由于liblz4.so.1位于/usr/lib/x86_64-linux-gnu,我尝试使用此功能:

g++ flann_example.cpp -I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial/lib -L/usr/lib/x86_64-linux-gnu -lhdf5 -llz4

可是:

/usr/bin/ld: cannot find -llz4
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:1)

由于系统中不存在liblz4.so,我不得不:

sudo apt-get install liblz4-dev

然后:

g++ flann_example.cpp -I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial/lib -L/usr/lib/x86_64-linux-gnu -lhdf5 -llz4

的工作。