无法用crfsuite的C++ API编译一个非常简单的Tagger示例

Posted

技术标签:

【中文标题】无法用crfsuite的C++ API编译一个非常简单的Tagger示例【英文标题】:Unable to compile a very simple example of Tagger with the C++ API of crfsuite 【发布时间】:2020-01-09 10:20:32 【问题描述】:

我已经从源代码 (https://github.com/downloads/chokkan/crfsuite/crfsuite-0.12.tar.gz) 正确安装了 crfsuite。 但是当我尝试编译一个非常简单的代码时,似乎我错过了一些东西。

代码如下:

#include "crfsuite.hpp"

using namespace CRFSuite;

int main(int argc, char *argv[]) 
 
    Tagger tagger;

这是要编译的命令行:

g++ -L/usr/local/lib -I/usr/local/include -lcrfsuite tagging.cpp

和错误:

/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::Trainer()':
tagging.cpp:(.text+0x48): undefined reference to `crfsuite_data_init'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::init()':
tagging.cpp:(.text+0x149): undefined reference to `crfsuite_create_instance'
tagging.cpp:(.text+0x1b7): undefined reference to `crfsuite_create_instance'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::clear()':
tagging.cpp:(.text+0x2dd): undefined reference to `crfsuite_data_finish'
tagging.cpp:(.text+0x2ed): undefined reference to `crfsuite_data_init'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::append(std::vector<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> >, std::allocator<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> > > > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, int)':
tagging.cpp:(.text+0x495): undefined reference to `crfsuite_instance_init_n'
tagging.cpp:(.text+0x51f): undefined reference to `crfsuite_item_init_n'
tagging.cpp:(.text+0x69b): undefined reference to `crfsuite_data_append'
tagging.cpp:(.text+0x6aa): undefined reference to `crfsuite_instance_finish'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::select(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tagging.cpp:(.text+0x7e9): undefined reference to `crfsuite_create_instance'
/tmp/ccIkvCFv.o: In function `CRFSuite::Tagger::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tagging.cpp:(.text+0x11a0): undefined reference to `crfsuite_create_instance_from_file'
/tmp/ccIkvCFv.o: In function `CRFSuite::Tagger::set(std::vector<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> >, std::allocator<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> > > > const&)':
tagging.cpp:(.text+0x16c6): undefined reference to `crfsuite_instance_init_n'
tagging.cpp:(.text+0x1731): undefined reference to `crfsuite_item_init'
tagging.cpp:(.text+0x17e1): undefined reference to `crfsuite_attribute_set'
tagging.cpp:(.text+0x17f4): undefined reference to `crfsuite_item_append_attribute'
tagging.cpp:(.text+0x1854): undefined reference to `crfsuite_instance_finish'
tagging.cpp:(.text+0x18ac): undefined reference to `crfsuite_instance_finish'
collect2: error: ld returned 1 exit status

路径正确(/usr/local/lib/usr/local/include

【问题讨论】:

【参考方案1】:

参数的顺序很重要。该库应该在您的 cpp 文件之后:

g++ -L/usr/local/lib -I/usr/local/include tagging.cpp -lcrfsuite

查看答案Why does the order in which libraries are linked sometimes cause errors in GCC?了解更多信息。

【讨论】:

非常感谢!我知道这是一个基本问题,但无法想象像这样解决它。我会记得的;-)

以上是关于无法用crfsuite的C++ API编译一个非常简单的Tagger示例的主要内容,如果未能解决你的问题,请参考以下文章

Mac 下 TensorFlow C++ API 的编译与测试

如何在VSCode内编译运行C++

Maya Plugin 编译Maya插件

c++项目中引入xgboost静态库

如何在 python crfsuite 中使用 Conll 2003 语料库

使用vs2019如何只编译一个c++文件