GCC:使用OpenSSL的SHA256函数 [英] GCC: Use OpenSSL's SHA256 Functions

查看:2375
本文介绍了GCC:使用OpenSSL的SHA256函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,让自己熟悉OpenSSL,libncurses和UDP网络。我决定与OpenSSL的SHA256合作,熟悉行业加密标准,但我遇到问题,使其工作。我已经隔离了OpenSSL与编译程序的链接的错误。我在Ubuntu 12.10,64位。我安装了libssl-dev包。

I'm writing a program to get myself acquainted with OpenSSL, libncurses, and UDP networking. I decided to work with OpenSSL's SHA256 to become familiar with industry encryption standards, but I'm having issues with getting it working. I've isolated the error to the linking of OpenSSL with the compiled program. I'm working on Ubuntu 12.10, 64 bit. I have the package libssl-dev installed.

例如,C ++ main.cpp:

Take, for instance, the C++ main.cpp:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

#include <openssl/sha.h>

string sha256(const string str)
{
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, str.c_str(), str.size());
    SHA256_Final(hash, &sha256);
    stringstream ss;
    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        ss << hex << setw(2) << setfill('0') << (int)hash[i];
    }
    return ss.str();
}

int main()
{

    cout << sha256("test") << endl;
    cout << sha256("test2") << endl;

    return 0;

}

我使用的是SHA256 href =http://stackoverflow.com/questions/2262386/generate-sha256-with-openssl-and-c>这里作为OpenSSL的SHA256功能的包装器。

I'm using the SHA256() function found here as a wrapper for OpenSSL's SHA256 functionality.

当我试图用下面的g ++参数编译时,我收到以下错误:

When I attempt to compile with the following g++ arguments, I receive the following error:

millinon@myhost:~/Programming/sha256$ g++ -lssl -lcrypto -o main main.cpp
/tmp/ccYqwPUC.o: In function `sha256(std::string)':
main.cpp:(.text+0x38): undefined reference to `SHA256_Init'
main.cpp:(.text+0x71): undefined reference to `SHA256_Update'
main.cpp:(.text+0x87): undefined reference to `SHA256_Final'
collect2: error: ld returned 1 exit status

识别OpenSSL定义的函数和类型,但ld未能找到sha.h中引用的函数符号。

So, GCC clearly recognizes OpenSSL's defined functions and types, but ld is failing to find the function symbols referred to in sha.h.

我需要手动指向一个特定的共享对象,目录?

Do I need to manually point to a specific shared object or directory?

谢谢!

推荐答案

错误...在构建时将在命令行上链接的库放在错误的位置。

You make a very common beginners mistake... Putting the libraries you link with in the wrong place on the command line when you build.

依赖在命令行上颠倒,因此依赖于

Dependencies are reversed on the command line, so something that depends on something else should actually be put before what it depends on on the command line.

在你的例子中,你有一个源文件 main.cpp 取决于一些库,那么源文件应该在它依赖的库之前:

In your example, you have a source file main.cpp that depends on some set of libraries, then the source file should be before the libraries it depend on:

$ g++ -o main main.cpp -lssl -lcrypto

是安全的,在命令行上列出的任何源或目标文件之后,始终将库置于。

To be safe, always put libraries last, after any source or object files listed on the command line.

这篇关于GCC:使用OpenSSL的SHA256函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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