在 C 中编译 md5 校验和但出现编译错误

compile md5 checksum in C but got compile error

尝试按照 SO example 中的示例进行操作,但在编译以下内容时出现错误:

$ gcc te.c
te.c: In function ‘main’:
te.c:10:17: error: storage size of ‘context’ isn’t known

代码如下:

#include <string.h>
#include <stdio.h>
#include <openssl/md5.h>
//#include <md5.h>


int main(int argc, char *argv[]) {
    unsigned char digest[16];
    const char* string = "Hello World";
    struct MD5_CTX context;
    MD5Init(&context);
    MD5Update(&context, string, strlen(string));
    MD5Final(digest, &context);
    int i;
    for (i=0; i<16; i++) {
        printf("%02x", digest[i]);
    }
    printf("\n");
    return 0;
}

顺便说一下,我的电脑是 运行 ubuntu 12.04 桌面。我的 gcc 版本是 4.7.3,这里是 libssl-dev

的版本
dpkg -l libssl-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
ii  libssl-dev     1.0.1-4ubuntu5 SSL development libraries, header files and

有什么想法吗?

更新1

感谢 Sourav Ghosh 指出,在上面,struct MD5_CTX context 中的 struct 应该被删除。结果函数名称也应该更改,例如 MD5InitMD5_Init

这是工作代码:

#include <string.h>
#include <stdio.h>
#include <openssl/md5.h>
//#include <md5.h>


int main(int argc, char *argv[]) {
    unsigned char digest[16];
    const char* string = "Hello World";
    MD5_CTX context;
    MD5_Init(&context);
    MD5_Update(&context, string, strlen(string));
    MD5_Final(digest, &context);
    int i;
    for (i=0; i<16; i++) {
        printf("%02x", digest[i]);
    }
    printf("\n");
    return 0;
}

要编译它,需要使用gcc te.c -lssl -lcrypto。 这也要感谢 SO answer

我认为,(和 I can seeMD5_CTX 已经是 typedefstruct。你不需要写struct MD5_CTX context;

将其更改为 MD5_CTX context; 应该可以。