【发布时间】:2012-12-27 02:05:06
【问题描述】:
我在我的 mac 上编译了一个正常工作的 md5 程序,但是当我尝试在我的 ubuntu 发行版上编译时,它出错了:
/tmp/ccKBJiV3.o: In function `str2md5':
md5.c:(.text+0x33): undefined reference to `MD5_Init'
md5.c:(.text+0x5b): undefined reference to `MD5_Update'
md5.c:(.text+0x79): undefined reference to `MD5_Update'
md5.c:(.text+0xa2): undefined reference to `MD5_Final'
collect2: ld returned 1 exit status
下面是我的 main 代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "md5.h"
#include <openssl/md5.h>
#include <openssl/hmac.h>
int main(int argc, char *argv[])
{
char *output = str2md5(argv[1], strlen(argv[1]));
printf("%s\n", output);
free(output);
return 0;
}
这是我的“md5.h”文件,它只包含 str2md5 函数:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif
char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
return out;
}
我试图用我在互联网上找到的所有 -l 东西来编译它。 例如:
gcc -Wall -lcrypto -lssl md5.c -o md5
任何帮助使它工作的帮助都将是惊人的!
【问题讨论】:
-
那是你的 md5.h header ??嗯。
-
@WhozCraig 这是什么意思?我做错了吗?
-
在你的标题中看到一个函数 definition 真是奇怪,这个地方通常用于 declarations,尤其是没有
inline序言。它会像你一样工作,因为它只包含在一个源文件中,但是一旦你将它拉入模块的其他源文件中(如果你添加了多个包含 md5.h 的 .c 文件header)你会从你的链接器中得到重复的符号错误。 -
@WhozCraig 啊,好吧。我明白你的意思。我很好奇你会把你的定义放在哪里?我还在学习,很想得到建议。