无法与库一起编译 [英] unable to compile with library

查看:73
本文介绍了无法与库一起编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用OpenALPR SDK,并编写了一些测试程序.问题是,我无法正确编译它,而且不确定为什么.这是 SDK文档.

I would like to experiment with the OpenALPR SDK and have written a little test program. The problem is, I can't get it to compile properly and I'm not sure why. Here's the SDK documentation.

我的源文件如下:

$ cat test.cpp 
#include <alpr.h>
#include <iostream>

std::string key =
"MyKey";

int main (void) 
{
    alpr::Alpr openalpr("us", "/etc/openalpr/openalpr.conf", "/usr/share/openalpr/runtime_data/", key);

    // Make sure the library loaded before continuing.  
    // For example, it could fail if the config/runtime_data is not found
    if (openalpr.isLoaded() == false)
    {
            std::cerr << "Error loading OpenALPR" << std::endl;
                return 1;
    }

    std::cout << "Hello World!" << std::endl;

    return 0;
}

我使用以下命令进行编译并获取输出:

I use the following command to compile and get the output:

$ g++ -Wall -lopenalpr test.cpp -o test
/tmp/ccGjLkrk.o: In function `main':
test.cpp:(.text+0xc5): undefined reference to `alpr::Alpr::Alpr(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x134): undefined reference to `alpr::Alpr::isLoaded()'
test.cpp:(.text+0x18e): undefined reference to `alpr::Alpr::~Alpr()'
test.cpp:(.text+0x1ce): undefined reference to `alpr::Alpr::~Alpr()'
test.cpp:(.text+0x202): undefined reference to `alpr::Alpr::~Alpr()'
test.cpp:(.text+0x236): undefined reference to `alpr::Alpr::~Alpr()'
test.cpp:(.text+0x273): undefined reference to `alpr::Alpr::~Alpr()'
/tmp/ccGjLkrk.o:test.cpp:(.text+0x290): more undefined references to `alpr::Alpr::~Alpr()' follow
collect2: error: ld returned 1 exit status

只需确认我的库应该在其中:libopenalpr.solibopenalpr.so.2的符号链接.

Just confirming my library is where it should be: libopenalpr.so is a symlink to libopenalpr.so.2.

$ locate libopenalpr.so
/usr/lib/libopenalpr.so
/usr/lib/libopenalpr.so.2

有人可以指出我在做什么错吗?

Can anyone point out what I'm doing wrong here?

推荐答案

gcc(1)手册页中:

... -l选项的位置很重要.

... the placement of the -l option is significant.

[...]

在命令中写入此选项的位置有所不同; 链接器在以下位置搜索和处理库和目标文件: 指定顺序.因此,foo.o -lz bar.o搜索库z 在文件foo.o之后但bar.o之前.如果bar.o引用了 z,这些功能可能无法加载.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

$ g++ -Wall test.cpp -lopenalpr -o test

这篇关于无法与库一起编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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