将挑战与gcc和加密库联系起来

时间:2014-03-08 12:37:32

标签: gcc openssl

我正在尝试用opneSSL编译示例应用程序,但我无法让链接器合作。需要一双新眼睛来看这个可能很简单的问题。请帮忙。

我试过阅读所有问题,但我找不到有什么问题。这是命令行和链接器输出

$ gcc  -t -I/usr/include/openssl -L/usr/lib/x86_64-linux-gnu -o algotest -lcrypto -lssl3 -lssl algotest.c

/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o
-lcrypto (/usr/lib/x86_64-linux-gnu/libcrypto.so)
-lssl3 (/usr/lib/x86_64-linux-gnu/libssl3.so)
-lssl (/usr/lib/x86_64-linux-gnu/libssl.so)
/tmp/ccSLR6rb.o
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
/lib/x86_64-linux-gnu/libc.so.6
(/usr/lib/x86_64-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o/tmp/ccSLR6rb.o: In function `main':
algotest.c:(.text+0x5c): undefined reference to `OpenSSL_add_all_digests'
algotest.c:(.text+0x99): undefined reference to `EVP_get_digestbyname'
algotest.c:(.text+0xdc): undefined reference to `EVP_MD_CTX_create'
algotest.c:(.text+0xfb): undefined reference to `EVP_DigestInit_ex'
algotest.c:(.text+0x13b): undefined reference to `EVP_DigestUpdate'
algotest.c:(.text+0x17b): undefined reference to `EVP_DigestUpdate'
algotest.c:(.text+0x192): undefined reference to `EVP_DigestFinal_ex'
algotest.c:(.text+0x19e): undefined reference to `EVP_MD_CTX_destroy'
/usr/bin/ld: link errors found, deleting executable `algotest'
collect2: ld returned 1 exit status

这是源代码......

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

main(int argc, char *argv[])
{
EVP_MD_CTX *mdctx;
const EVP_MD *md;
char mess1[] = "Test Messagen";
char mess2[] = "Hello Worldn";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

if(!argv[1]) {
printf("Usage: mdtest digestnamen");
exit(1);
}

md = EVP_get_digestbyname(argv[1]);

if(!md) {
printf("Unknown message digest %sn", argv[1]);
exit(1);
}

mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("n");
}

2 个答案:

答案 0 :(得分:4)

  

$ gcc -t -I / usr / include / openssl -L / usr / lib / x86_64-linux-gnu -o algotest -lcrypto -lssl3 -lssl algotest.c

尝试:

$ gcc algotest.c -o algotest -lssl  -lcrypto

由于您使用#include <openssl/evp.h>,您可以:

$ gcc algotest.c -o algotest -lssl  -lcrypto

另外,请确保安装了OpenSSL的开发版本。在Ubuntu上,命令是:

$ sudo apt-get install libssl-dev

在Fedora上,我相信命令是:

$ sudo yum install openssl-devel

答案 1 :(得分:1)

我发现了问题!归功于@Marc指出它。 C文件应该在-l文件之前。