链接器错误编译keyczar程序

时间:2013-01-22 18:29:50

标签: c++ linux linker g++ static-libraries

我使用g++ -lkeyczar -lcrypto -o basic_encrypt -Wall -O2 base_encrypt.cpp编译以下代码:

#include <cassert>
#include <iostream>
#include <string>
#include <keyczar/keyczar.h>

void EncryptAndDecrypt(const std::string& location) {
  keyczar::Keyczar* crypter = keyczar::Crypter::Read(location);
  if (!crypter)
    return;

  std::string input = "Secret message";
  std::string ciphertext;
  std::cout << "Plaintext: " << input << std::endl;

  bool result = crypter->Encrypt(input, &ciphertext);
  if (result) {
    std::cout << "Ciphertext (Base64w): " << ciphertext << std::endl;
    std::string decrypted_input;
    bool result = crypter->Decrypt(ciphertext, &decrypted_input);
    if (result)
      assert(input == decrypted_input);
  }
  delete crypter;
}

int main(int argc, char** argv) {
  if (argc != 2) {
    std::cout << "An absolute key set location must be provided as argument"
              << std::endl;
    return 1;  // error
  }

  // The first argument must represent the keyset's location
  const std::string location(argv[1]);

  EncryptAndDecrypt(location);
  return 0;
}

这是从here

获取的教程

但是,我遇到了以下错误:

/tmp/ccNlack3.o: In function `EncryptAndDecrypt(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
base_encrypt.cpp:(.text+0xf): undefined reference to `keyczar::Crypter::Read(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: ld returned 1 exit status

我无法解决这个问题因为我知道我在编译时已经提供了库标志。为什么它仍然无法正确链接?

1 个答案:

答案 0 :(得分:4)

将库标志放在命令行的末尾:

g++ -o basic_encrypt -Wall -O2 base_encrypt.cpp -lkeyczar -lcrypto