gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:58:36 24 4
gpt4 key购买 nike

我正在尝试使用 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");
}

最佳答案

$ 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

关于gcc - 将挑战与 gcc 和加密库联系起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22269408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com