Cygwin gcc compiled fails in IDE complaining about 'exit' undeclared

Verhogen picture Verhogen · Feb 7, 2010 · Viewed 7.5k times · Source

When I compile a program using just

gcc code.c

There are no messages, and an output file is generated successfully. The outputted file works. However, when I try to the same cygwin installation's gcc compiler in an IDE (I've tried Netbeans and Dev-C++), I get the following errors

main.cpp:27: error: `exit' undeclared (first use this function)
main.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:77: error: `write' undeclared (first use this function)
main.cpp:78: error: `close' undeclared (first use this function)

I don't see what's different. Why does it not compile?

OK, the issue was that in the IDE, the file had a .cpp extension, whereas when I was compiling from a terminal, it had a .c extension. So, my new question is why does it not compile when it's treated as a c++ file. Isn't C a subset of C++?

Answer

R Samuel Klatchko picture R Samuel Klatchko · Feb 8, 2010

C++ is stricter then C. Where C allows you to call a function without a prototype, C++ does not allow this.

To solve the problem, you want to add:

#include <stdlib.h>

Also, when compiling at the command line. Make sure to use the -Wall flag so you'll get important warnings:

gcc -Wall code.c