【问题标题】:Linker error compiling keyczar program链接器错误编译 keyczar 程序
【发布时间】:2013-01-22 18:29:50
【问题描述】:

我正在使用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

我无法解决这个问题,因为我知道我已经在编译时给出了库标志。为什么还是不能正确链接?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,问题解决了,不过现在我有了一个新的:./basic_encrypt: error while loading shared libraries: libkeyczar.so: cannot open shared object file: No such file or directory 我知道安装并没有将libkeyczar.so 文件放在通常的/usr/lib 文件夹中,而是在另一个位置(在同一个源文件夹内..)但无论如何,我现在必须做什么才能让 g++ 找到 libkeyczar.so ?我知道它存储在哪里。我如何告诉 G++ 它的路径?
    • 好的,我通过`LD_LIBRARY_PATH=/home/r/l33t/Security_Model/keyczar-cpp/src/scons-out/opt-linux/lib ./basic_encrypt`解决了这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多