使用 Open SSL 运行 C 代码
我无法编译以下代码。我在 MACOSX 中使用终端中的以下命令运行它:
$gcc filename.c -lssl
我知道 MACOS 已经有一个内置的 openssl (我可以在终端中作为命令调用)。但我不确定我是否以正确的方式将其链接到库。
#include <stdio.h>
#include <openssl/evp.h>
main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;
OpenSSL_add_all_digests();
if(!argv[1]) {
printf("Usage: mdtest digestname\n");
exit(1);
}
md = EVP_get_digestbyname(argv[1]);
if(!md) {
printf("Unknown message digest %s\n", argv[1]);
exit(1);
}
EVP_MD_CTX_init(&mdctx);
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_cleanup(&mdctx);
printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}
我的错误是:
未定义的符号: “_EVP_MD_CTX_cleanup”,引用自:ccfZG7WJ.o中的_main
ld:未找到符号 collect2: ld returned 1 exit status
我对这个错误感到惊讶,因为它显示“未定义的符号”而不是“找不到头文件”。你能帮我调试这个问题吗?
谢谢!
I am not able to compile the following code. I run it in a MACOSX with the following command in the terminal:
$gcc filename.c -lssl
I understand that MACOS already has an inbuilt openssl (which i can call as a command in the terminal). But I am not sure if i am linking it to the library in the correct fashion.
#include <stdio.h>
#include <openssl/evp.h>
main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;
OpenSSL_add_all_digests();
if(!argv[1]) {
printf("Usage: mdtest digestname\n");
exit(1);
}
md = EVP_get_digestbyname(argv[1]);
if(!md) {
printf("Unknown message digest %s\n", argv[1]);
exit(1);
}
EVP_MD_CTX_init(&mdctx);
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_cleanup(&mdctx);
printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}
My error is:
Undefined symbols:
"_EVP_MD_CTX_cleanup", referenced from: _main in ccfZG7WJ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I am surprised by the error since it shows "Undefined Symbols" instead of "Header file not found". Can you please help me debug this issue.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OpenSSL 有 2 个库:
libssl
和libcrypto
。也包括 libcrypto。(实际上,这个程序根本不需要
-lssl
,但包含它也没什么坏处)libssl 用于建立实际的 SSL 或 TLS 连接,例如使用 https 连接到 Web 服务器时。它还导出
SSL_load_error_strings
,即使仅使用加密库,这也很有用。libcrypto 是密码学库。它具有加密算法、密钥和证书生成与验证功能、消息认证算法等。
OpenSSL has 2 libraries:
libssl
andlibcrypto
. Include libcrypto, too.(You actually don't need
-lssl
at all for this program, but it doesn't hurt to include it)libssl is for making actual SSL or TLS connections, like when connecting to a web server with https. It also exports
SSL_load_error_strings
, which is useful even when working only with the cryptography library.libcrypto is the cryptography library. It has encryption algorithms, key and certificate generation and verification functions, message authentication algorithms, and so on.
您怀疑该库未正确链接是正确的。有两种可能性:
第二个似乎更有可能,但第一个更容易检查。
man -k ssl
应该指出库手册页。You're right to suspect the library isn't being linked correctly. There are two possibilities:
The second one seems more probable, but the first is easier to check. A
man -k ssl
should point out the library man pages.在您的情况下,“未定义的符号”很可能意味着您没有链接正确的库。
Most probably in your case "Undefined Symbols" means that you are not linking the correct library.