分享

undefined reference to `SHA1’的处理方法 | 大和尚's blog

 dwlinux_gs 2015-06-24

在linux编程使用openssl库时, 有时候编译文件会出现

undefined reference to SHA1 等类似错误

而SHA1在openssl/sha.h文件中已经定义,且在自己的c文件中也include了该openssl/sha.h !!

 

这时候,在编译的时候应该加上-lcrypto -lssl 选项,即可消除编译错误.

例子: c文件:

#include<stdio.h>
#include<stdint.h>
#include <openssl/sha.h>

int main(int argc,char **argv)

{
printf(“hello world\n”);

unsigned char hash[SHA_DIGEST_LENGTH];

unsigned char content[4096] = {‘\x41′};

uint32_t len = 4096;

unsigned char * res = SHA1(content,len,hash);

printf(“digest length = %d\n”,SHA_DIGEST_LENGTH);

printf(“digest hash = %s”,hash);

return 0;
}

编译方法:

gcc -o use_sha use_sha.c -lcrypto -lssl

运行:

./use_sha
hello world
digest length = 20
digest hash = ??7_?9J???Hs???Pu

已无错误!! 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多