Is it possible to put #ifndef
at the top of a c file? Basically I need to check whether a certain preprocessor constant was declared when running the program and my program will change accordingly.
I need to check if -D DESCENDING_ORDER=1
is added as an argument (doesn't matter what value given).
I have this code at the top of my main c file:
#ifndef DESCENDING_ORDER
int ascending = 1;
#else
int ascending = 0;
#endif
Works when compiling by itself, but I get errors when I try compiling with a Makefile, something along the lines of "expected identifier before 'int' for int ascending = 1
.
Thanks.
EDIT - Added Makefile code
CC=gcc
CFLAGS=-g -Wall
INC=-include
RES_OBS=res.o
LIBS=
all: res
res: $(RES_OBS)
$(CC) $(CFLAGS) -o res $(RES_OBS) $(LIBS) $(INC) res.h -D DESCENDING_ORDER=1
clean:
rm -f *.o
clobber:
make clean
rm -f res
Kind of guessed and added $(INC)....DESCENDING_ORDER=1
to the end of the command, so that's probably why it's not working.
Command I'm using without makefile:
gcc res -include res.h -D DESCENDING_ORDER=1
EDIT 2 - Had a little play with different arguments and found that I get the same error if I remove -include res.h
in the command. Still not sure how to correctly reference the header file in the makefile? I've added the #include "res.h"
in my res.c file but still get the error.