md5引用错误

Bry*_*yce 5 c openssl md5

我在我的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
Run Code Online (Sandbox Code Playgroud)

以下是我的主要代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的"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;
} 
Run Code Online (Sandbox Code Playgroud)

我试图用我在互联网上找到的所有东西来编译它.例如:

 gcc -Wall -lcrypto -lssl md5.c -o md5
Run Code Online (Sandbox Code Playgroud)

任何有助于实现这一目标的帮助都会令人惊叹!

小智 18

OS X使用古老版本的GNU工具链,而Ubuntu(以及一般的Linux发行版)使用较新版本.这些更新的版本要求按照符号相互依赖的顺序指定对象和库文件.这意味着为了获得最大的可移植性,您应该始终将库链接器标志放在命令行调用的末尾,如下所示:

gcc -Wall md5.c -o md5 -lcrypto -lssl
Run Code Online (Sandbox Code Playgroud)


Ant*_*nko 6

放在-l命令的末尾:

gcc -Wall md5.c -o md5 -lcrypto -lssl
Run Code Online (Sandbox Code Playgroud)