kpu / kenlm

KenLM: Faster and Smaller Language Model Queries
http://kheafield.com/code/kenlm/
Other
2.5k stars 513 forks source link

compile interface example demo error #292

Closed bird8693 closed 4 years ago

bird8693 commented 4 years ago

I compile the code of interface example, however it throw many error: g++ -o example exmaple.c

#include "lm/model.hh"
#include <iostream>
#include <string>
int main() {
  using namespace lm::ngram;
  Model model("file.arpa");
  State state(model.BeginSentenceState()), out_state;
  const Vocabulary &vocab = model.GetVocabulary();
  std::string word;
  while (std::cin >> word) {
    std::cout << model.Score(state, vocab.Index(word), out_state) << '\n';
    state = out_state;
  }
}

Errror:

LM_MAX_ORDER=6 -DHAVE_XZLIB
/tmp/ccDKZ91j.o: In function `main':
xx.cc:(.text+0x26): undefined reference to `lm::ngram::Config::Config()'
/tmp/ccDKZ91j.o: In function `util::scoped_memory::reset()':
xx.cc:(.text._ZN4util13scoped_memory5resetEv[_ZN4util13scoped_memory5resetEv]+0x23): undefined reference to `util::scoped_memory::reset(void*, unsigned long, util::scoped_memory::Alloc)'
/tmp/ccDKZ91j.o: In function `lm::ngram::detail::HashForVocab(StringPiece const&)':
xx.cc:(.text._ZN2lm5ngram6detail12HashForVocabERK11StringPiece[_ZN2lm5ngram6detail12HashForVocabERK11StringPiece]+0x2f): undefined reference to `lm::ngram::detail::HashForVocab(char const*, unsigned long)'
/tmp/ccDKZ91j.o: In function `lm::ngram::BinaryFormat::~BinaryFormat()':
xx.cc:(.text._ZN2lm5ngram12BinaryFormatD2Ev[_ZN2lm5ngram12BinaryFormatD5Ev]+0x49): undefined reference to `util::scoped_fd::~scoped_fd()'
xx.cc:(.text._ZN2lm5ngram12BinaryFormatD2Ev[_ZN2lm5ngram12BinaryFormatD5Ev]+0x88): undefined reference to `util::scoped_fd::~scoped_fd()'
/tmp/ccDKZ91j.o: In function `lm::ngram::ProbingModel::ProbingModel(char const*, lm::ngram::Config const&)':
xx.cc:(.text._ZN2lm5ngram12ProbingModelC2EPKcRKNS0_6ConfigE[_ZN2lm5ngram12ProbingModelC5EPKcRKNS0_6ConfigE]+0x27): undefined reference to `lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>::GenericModel(char const*, lm::ngram::Config const&)'
/tmp/ccDKZ91j.o: In function `lm::base::ModelFacade<lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>, lm::ngram::State, lm::ngram::ProbingVocabulary>::~ModelFacade()':
xx.cc:(.text._ZN2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_ED2Ev[_ZN2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_ED5Ev]+0x20): undefined reference to `lm::base::Model::~Model()'
/tmp/ccDKZ91j.o: In function `lm::base::ModelFacade<lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>, lm::ngram::State, lm::ngram::ProbingVocabulary>::Score(lm::ngram::State const&, unsigned int, lm::ngram::State&) const':
xx.cc:(.text._ZNK2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_E5ScoreERKSA_jRSA_[_ZNK2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_E5ScoreERKSA_jRSA_]+0x40): undefined reference to `lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>::FullScore(lm::ngram::State const&, unsigned int, lm::ngram::State&) const'
/tmp/ccDKZ91j.o:(.rodata._ZTIN2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_EE[_ZTIN2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_EE]+0x10): undefined reference to `typeinfo for lm::base::Model'
/tmp/ccDKZ91j.o:(.rodata._ZTIN2lm5ngram17ProbingVocabularyE[_ZTIN2lm5ngram17ProbingVocabularyE]+0x10): undefined reference to `typeinfo for lm::base::Vocabulary'
/tmp/ccDKZ91j.o: In function `lm::ngram::ProbingVocabulary::~ProbingVocabulary()':
xx.cc:(.text._ZN2lm5ngram17ProbingVocabularyD2Ev[_ZN2lm5ngram17ProbingVocabularyD5Ev]+0x20): undefined reference to `lm::base::Vocabulary::~Vocabulary()'
/tmp/ccDKZ91j.o: In function `lm::base::ModelFacade<lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>, lm::ngram::State, lm::ngram::ProbingVocabulary>::BaseFullScore(void const*, unsigned int, void*) const':
xx.cc:(.text._ZNK2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_E13BaseFullScoreEPKvjPv[_ZNK2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_E13BaseFullScoreEPKvjPv]+0x44): undefined reference to `lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>::FullScore(lm::ngram::State const&, unsigned int, lm::ngram::State&) const'
/tmp/ccDKZ91j.o: In function `lm::base::ModelFacade<lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>, lm::ngram::State, lm::ngram::ProbingVocabulary>::BaseFullScoreForgotState(unsigned int const*, unsigned int const*, unsigned int, void*) const':
xx.cc:(.text._ZNK2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_E24BaseFullScoreForgotStateEPKjSD_jPv[_ZNK2lm4base11ModelFacadeINS_5ngram6detail12GenericModelINS3_12HashedSearchINS2_12BackoffValueEEENS2_17ProbingVocabularyEEENS2_5StateES8_E24BaseFullScoreForgotStateEPKjSD_jPv]+0x50): undefined reference to `lm::ngram::detail::GenericModel<lm::ngram::detail::HashedSearch<lm::ngram::BackoffValue>, lm::ngram::ProbingVocabulary>::FullScoreForgotState(unsigned int const*, unsigned int const*, unsigned int, lm::ngram::State&) const'
collect2: error: ld returned 1 exit status
kpu commented 4 years ago

You need to link it against the library and the libraries used. Here's an example, presuming you've compiled everything and have all the compression libraries installed.

g++ -DKENLM_MAX_ORDER=6 -o example example.cc -I. -L build/lib -lkenlm -lkenlm_util -llzma -lbz2 -lz