1

This is the first time I've used Eclipse for writing C code, so the answer might be very simple. I'm also figuring that Eclipse is already setup to compile, build, and link appropriately. This is being ran on a Windows 7 64-bit machine.

In my C code, it seems that anytime I use the malloc() or even printf() function it puts the red squiggly line below it. Just the function is underlined, not the casting or even the parameters.

Here is what the line of code:

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

When I hover my mouse over the error, it says the following:

Function 'malloc' could not be resolved.

Am I even using malloc() right?

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
  • 2
    Are you remembering to `#include `? – Graham Borland Mar 06 '13 at 22:01
  • 4
    If you're writing C (not C++), you're not doing this right - [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Philip Kendall Mar 06 '13 at 22:01
  • what error do you get? If it is underlined with red it should be an error there. – niculare Mar 06 '13 at 22:01
  • @GrahamBorland yes, `#include ` is included. Made sure of that. – Rob Avery IV Mar 06 '13 at 22:01
  • Does it still compile, regardless of the hover message? – teppic Mar 06 '13 at 22:06
  • @teppic Nope. Doesn't compile. – Rob Avery IV Mar 06 '13 at 22:09
  • I thought it was `malloc.h` that you needed to include... – Bingo Mar 06 '13 at 22:10
  • 1
    What's the error message in the console window when you try to compile? – teppic Mar 06 '13 at 22:11
  • Is this an Android project? See http://stackoverflow.com/questions/8686931/eclipse-cdt-fails-to-find-stl-symbols-in-ndk-project – Graham Borland Mar 06 '13 at 22:11
  • 1
    I deleted my answer because it was just worth a comment, but I don't know, I've always been told not to cast my `malloc`'s results : `int* list; list = malloc(sizeof(int));` – Rob Mar 06 '13 at 22:18
  • I'm going to guess that it's because `stdlib.h` isn't installed (e.g. on Linux the development headers aren't properly installed). That'd be clear from the compiler error though. If not though, what's the platform? Which compiler? – teppic Mar 06 '13 at 22:25
  • @teppic How do I install the compiler onto Eclipse? – Rob Avery IV Mar 06 '13 at 22:25
  • @teppic Or install any of the `*.h`s like `stdlib.h` – Rob Avery IV Mar 06 '13 at 22:26
  • @RobAveryIV: you need to supply more info. Are you using Windows, OS X or Linux? And if Linux, which distribution? – teppic Mar 06 '13 at 22:27
  • @teppic On a Windows 7 64-bit machine. I added it to my answer. – Rob Avery IV Mar 06 '13 at 22:28
  • Is this snippet *inside the body of a function?*. Function calls (and other code) are not allowed in global scope. – wildplasser Mar 06 '13 at 22:28
  • @wildplasser the pointer is a global variable, but the malloc function isn't. The malloc function is inside a function. – Rob Avery IV Mar 06 '13 at 22:30
  • @RobAveryIV: Can you compile any standard library functions (e.g. `printf`) or is it just `malloc` that gives a problem? When you compile the project the console window at the bottom will show the output from the compilation, and the compiler error -- that's needed. – teppic Mar 06 '13 at 22:31
  • 1
    Why don't you show the real code, instead of forcing us to ask obvious questions, or guess the missing parts ? – wildplasser Mar 06 '13 at 22:32
  • the code compiles without the malloc? Eclipse find stdlib.h? – nap.gab Mar 06 '13 at 22:34
  • Well, obviously Eclipse (just like the compiler) must have a means to enable it to locate the directories with the header files. Could be an environment variable, configuration setting, or a config file. – wildplasser Mar 06 '13 at 22:47
  • Your call is legal. As a matter of style, it would be better written as `list = malloc(sizeof *list);`, but that doesn't explain the complaint you're getting. But regardless of the red squiggly line, does your code compile and run? (`` is a C standard header; if Eclipse can't find it, it's badly misconfigured.) – Keith Thompson Mar 06 '13 at 22:48
  • @Bingo: No, there is no standard `malloc.h` header. The ISO C standard says that `malloc` is declared in ``. – Keith Thompson Mar 06 '13 at 22:49

2 Answers2

2

You haven't provided enough information. You need to provide a complete program including malloc, however small, that doesn't compile. You also need to give the compiler error you get, and whether the problem is just with malloc or all standard library functions.

At a guess, your compiler software isn't installed correctly and it can't find stdlib.h.

As you've now added that no standard library functions work, there's definitely a problem with your compiler set up (this is nothing to do with malloc).

teppic
  • 8,039
  • 2
  • 24
  • 37
  • I think your right. How can I do that, or can you point me in a good direction? – Rob Avery IV Mar 06 '13 at 22:49
  • @RobAveryIV Check your compiler's fully installed. You haven't said which it is - gcc? A plugin for Visual Studio? If the question is more 'how do I install eclipse on Windows for C/C++', it's a bit too broad for this question. – teppic Mar 06 '13 at 22:51
1

According to the clang and the cc compilers, your code is ok and it compiles in Eclipse Juno:

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

I think you can look at this question

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

if you want a complete solution.

Community
  • 1
  • 1
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424