C语言 md5 参考错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14295980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
md5 reference error
提问by Bryce
I had a correctly working md5 program compiled on my mac but when I try to compile on my ubuntu distro it errors out saying:
我在我的 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
Below is my code for 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;
}
Here is my "md5.h" file just contains the str2md5 function:
这是我的“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;
}
I have tried to compile it with all the -l things I have found on the internet. For example:
我试图用我在互联网上找到的所有 -l 东西来编译它。例如:
gcc -Wall -lcrypto -lssl md5.c -o md5
Any help to get this to work would be amazing!
任何帮助使其正常工作都将是惊人的!
回答by
OS X uses an ancient version of the GNU toolchain, whereas Ubuntu (and Linux distros in general) uses a newer one. These more recent versions require that the object and library files be specified in the order their symbols depend on each other. It means that for maximum portability, you should always put the library linker flags to the end of the command line invocation,like this:
OS X 使用古老版本的 GNU 工具链,而 Ubuntu(以及一般的 Linux 发行版)使用更新的工具链。这些较新的版本要求按照它们的符号相互依赖的顺序指定对象和库文件。这意味着为了获得最大的可移植性,您应该始终将库链接器标志放在命令行调用的末尾,如下所示:
gcc -Wall md5.c -o md5 -lcrypto -lssl
回答by Anton Kovalenko
Put -lat the end of your command:
放在-l命令的末尾:
gcc -Wall md5.c -o md5 -lcrypto -lssl
回答by stanri
To do this in eclipse:
要在 Eclipse 中执行此操作:
Right Click Project in Eclipse-> C/C++ Build Settings -> Tool Settings -> GCC C Linker -> Libraries and then add "ssl" and "crypto" in the -l section on the right.
在 Eclipse 中右键单击 Project-> C/C++ Build Settings -> Tool Settings -> GCC C Linker -> Libraries,然后在右侧的 -l 部分添加“ssl”和“crypto”。
回答by Mansur Ali
on ubuntu 16.04 i was having issues then i compiled it on 14.04 and it worked for me
在 ubuntu 16.04 上我遇到了问题,然后我在 14.04 上编译了它,它对我有用
回答by Kemin Zhou
Sometimes, proper symbolic link may be needed:
有时,可能需要正确的符号链接:
My initial system file:
我的初始系统文件:
-rwxr-xr-x. 1 root root 1408384 Jun 5 2014 libcrypto.so.0.9.8e
lrwxrwxrwx. 1 root root 19 Sep 22 2015 libcrypto.so.10 -> libcrypto.so.1.0.1e
-rwxr-xr-x. 1 root root 1965856 Jul 23 2015 libcrypto.so.1.0.1e
lrwxrwxrwx. 1 root root 19 Sep 22 2015 libcrypto.so.6 -> libcrypto.so.0.9.8e
lrwxrwxrwx. 1 root root 25 Sep 25 2015 libcrypt.so -> ../../lib64/libcrypt.so.1
Only after I have added the symbolic link
只有在我添加了符号链接之后
sudo ln -s libcrypto.so.1.0.1e libcrypto.so
须藤ln -s libcrypto.so.1.0.1e libcrypto.so
My linking started to work
我的链接开始工作


