【问题标题】:Using malloc() and other functions in C using Eclipse使用 Eclipse 在 C 中使用 malloc() 和其他函数
【发布时间】:2013-02-21 22:15:30
【问题描述】:

这是我第一次使用 Eclipse 编写 C 代码,所以答案可能很简单。我还认为 Eclipse 已经设置为可以适当地编译、构建和链接。这是在 Windows 7 64 位机器上运行的。

在我的 C 代码中,似乎每当我使用 malloc() 甚至 printf() 函数时,它都会在其下方放置红色波浪线。只是函数有下划线,而不是转换甚至参数。

下面是这行代码:

#include <stdlib.h>
#include <stdio.h>

int* list; //This is a global variable

int main(){

    //...inside a function
    list = (int*) malloc( sizeof(int) ); // Out of this line, only the word "malloc" is underlined in red.
    printf(""); //printf also gives me an error

    return 0;
}

当我将鼠标悬停在错误上时,它会显示以下内容:

函数“malloc”无法解析。

我什至使用malloc() 对吗?

【问题讨论】:

  • 你还记得#include &lt;stdlib.h&gt;吗?
  • 如果您正在编写 C(不是 C++),那么您做的不对 - Do I cast the result of malloc?
  • 尝试编译时控制台窗口中的错误信息是什么?
  • 我删除了我的答案,因为它只是值得评论,但我不知道,我一直被告知不要投我的 malloc 的结果:int* list; list = malloc(sizeof(int));
  • 你为什么不展示真实的代码,而不是强迫我们问明显的问题,或者猜测缺失的部分?

标签: c eclipse eclipse-plugin eclipse-cdt


【解决方案1】:

您没有提供足够的信息。您需要提供一个完整的程序,包括malloc,无论多么小,都无法编译。你还需要给出你得到的编译器错误,以及问题是出在malloc 还是所有标准库函数。

您的编译器软件安装不正确,找不到stdlib.h

正如您现在添加的,没有标准库函数可以工作,您的编译器设置肯定有问题(这与 malloc 无关)。

【讨论】:

  • 我认为你是对的。我该怎么做,或者你能给我指明一个好的方向吗?
  • @RobAveryIV 检查您的编译器是否已完全安装。你还没有说它是哪个 - gcc? Visual Studio 的插件?如果问题更多的是“如何在 Windows 上为 C/C++ 安装 eclipse”,那么这个问题就有点过于宽泛了。
【解决方案2】:

根据 clang 和 cc 编译器,您的代码没有问题,并且可以在 Eclipse Juno 中编译:

Building target: code
Invoking: GCC C Linker
gcc  -o "code"  ./code.o   
Finished building target: code

我觉得你可以看看这个问题

"Unresolved inclusion" error with Eclipse CDT for C standard library headers

如果您想要一个完整的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2022-07-12
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多