MD5引用错误 [英] md5 reference error

查看:1322
本文介绍了MD5引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Mac上编制了正常工作MD5程序,但是当我尝试编译在我的Ubuntu发行版,它的错误出来说:

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:

 /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

下面是我的code主:

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;
}

下面是我的md5.h文件只包含str2md5功能:

Here is my "md5.h" file just contains the str2md5 function:

#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事情,我已经在互联网上找到编译它。
例如:

I have tried to compile it with all the -l things I have found on the internet. For example:

 gcc -Wall -lcrypto -lssl md5.c -o md5

任何帮助得到这个工作将是惊人的!

Any help to get this to work would be amazing!

推荐答案

OS X使用GNU工具链的一个古老的版本,而Ubuntu的(一般和Linux发行版),采用较新的。这些较新版本的要求,对象和库文件中的符号互相依赖的顺序指定。这意味着,最大的可移植性,您应该始终把库连接标志命令行调用结束,是这样的:

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:

gcc -Wall md5.c -o md5 -lcrypto -lssl

这篇关于MD5引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆