【问题标题】:compile md5 checksum in C but got compile error在 C 中编译 md5 校验和但得到编译错误
【发布时间】:2015-06-01 13:38:52
【问题描述】:

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

$ gcc te.c
te.c:在函数'main'中:
te.c:10:17:错误:“上下文”的存储大小未知

代码如下:

#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 应该被删除。原来函数名也应该改一下,比如MD5Init改成MD5_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

【问题讨论】:

  • struct MD5_CTX 定义在哪里?
  • 我认为应该在 openssl/md5.h 中定义。虽然没有检查。
  • 能找到头文件,否则编译器会报找不到。
  • 我不认为按照最新问题的方式编辑问题是个好主意。一个问题,应该仍然是一个问题。恕我直言,您应该回滚到以前的版本。

标签: c struct compiler-errors md5


【解决方案1】:

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

将其更改为MD5_CTX context;,它应该可以工作。

【讨论】:

  • 是的,你是对的。我也刚刚意识到这一点。将在几分钟内接受您的回答,因为 SO 阻止我现在这样做。顺便说一句,函数应该是MD5_InitMD5_UpdateMD5_Final,但是当我运行gcc te.c -lssl时链接器仍然找不到它。有什么想法吗?
  • @codingFun 这与我之前的评论相同,您是否提供了库的路径?
  • 是的,我认为默认值应该可以工作,因为如果它能够找到 libssl.a(命令行中的 -lssl 选项)。否则,链接器会抱怨找不到库。
  • @codingFun 您可以尝试使用-L 选项明确提供库的路径吗?到目前为止,我没有看到任何其他原因。
  • 我看到了问题:错过了加密。这有效:gcc te.c -lssl -lcrypto。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2013-10-31
相关资源
最近更新 更多