mewais
mewais

Reputation: 1337

gcc compiler error with -D option

I've written a makefile that compiles multiple files. when executing a line like this :

g++ -c -Wall -U DEBUG -U FILE -U HighPriority -U OnlyCUDA -U CUDA -U THREAD_NUM -U SIZE -U InputFileName -D FILE -D SIZE=32 -D THREAD_NUM=4 -D CUDA -D InputFileName=input/In32.txt  ../src/lib/Globals.cpp -o Globals.o

It generates a huge list of errors:

In file included from /usr/include/wchar.h:36:0,
                 from /usr/include/c++/4.6/cwchar:46,
                 from /usr/include/c++/4.6/bits/postypes.h:42,
                 from /usr/include/c++/4.6/iosfwd:42,
                 from /usr/include/c++/4.6/ios:39,
                 from /usr/include/c++/4.6/istream:40,
                 from /usr/include/c++/4.6/sstream:39,
                 from /usr/include/c++/4.6/complex:47,
                 from ../src/lib/../inlcude/Globals.h:3,
                 from ../src/lib/Globals.cpp:1:
/usr/include/stdio.h:48:25: error: expected unqualified-id before numeric constant

but when I remove -D FILE it compiles just fine. what is this about ??

EDIT1: the same #define FILE works fine when I use codeblocks for example. why so ??

Upvotes: 2

Views: 109

Answers (2)

trojanfoe
trojanfoe

Reputation: 122391

FILE is already used in C as a file pointer object (see the fopen(3) manpage).

You will need to choose a different name for that constant.

The actual error is caused by the declaration of functions like fopen():

FILE *fopen(const char *restrict filename, const char *restrict mode);

being turned into:

*fopen(const char *restrict filename, const char *restrict mode);

because you defined FILE to nothing.

EDIT Actually it's probably causing an issue in the declaration of FILE itself, and not the fopen() et al functions:

typedef struct __sFILE {
    ...
} FILE;

Where FILE is replaced with nothing.

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

Defining FILE is a very bad idea*, because it already exists as a type in the standard library!


* AKA "invoking undefined behaviour".

Upvotes: 1

Related Questions