Can't fix id problem in NetBeans

I am trying to set up the NetBeans 8.1 beta for the first time to run my C codes. It looks like I cannot solve the unknown ID problem! The versions are shown below:

enter image description here

Following screenshot from main code:

enter image description here

Despite the error, the code compiles successfully. As I understand it, I entered the folder include

into NetBeans (see below), but it looks like I am doing something wrong because the problem still persists.

enter image description here

I re-parsed the project as recommended here and manually added the directory include

to my project properties.

enter image description here

But there is still no success. What else do I need to do?

+3


source to share


1 answer


This is actually a rather fascinating little historical quirk of C that I suspect you are running into. Basically what happens is Netbeans only parses the included header files to figure out what features have been declared. Since you didn't include the header for sleep (unistd.h), Netbeans is unaware of this and thus confused.

Now this prompts the question of how this happens in the world. Well that's where it gets interesting. It turns out in C there are implicit function declarations . In other words, if you use a function without declaring it, the compiler will just keep trucking around and assume that it accepts arguments of whatever type you gave it and that it returns an int.

Fortunately, it turns out that the real version of sleep is compatible for linking to that implicit version, so when linking to the default libraries, you get lucky and it finds sleep

.

Note. The warning from Netbeans is correct. You get caught up in "it works for you, but it's not guaranteed to work" (welcome to C!), And you should try your best to avoid it. The quickest step you can take in this situation is to check the warnings.

With appropriate warning levels, you will see something like this:



test.c: In function 'main':
test.c:4:2: warning: implicit declaration of function 'sleep' [-Wimplicit-function-declaration]
  sleep(5);
  ^

      

That will later tell you what you need #include <unistd.h>

.


tl; dr: add #include <unistd.h>

and wrap your warnings (something like -Wall -Wextra

a minimum) no matter how Netbeans does it.

+2


source







All Articles