Program compiled on Linux not getting code in #ifdef section
I have a cross platform program I am working on that has several files with
#ifdef WIN32
...inclues
#endif
#ifdef LINUX
..inlcudes
#endif
when I went to compile on LINUX I keep getting errors about not finding functions and such, but if i comment out the #ifdef LINUX blocks (still keeping the includes uncommented) it works, so it seams as if my define is not working properly.
This is my make file (file names changed):
CC = gcc
CPP = g++
CFLAGS = -DLINUX $(INCLUDES) -c -g -O0 -开发者_开发知识库Wall -fpic
INCLUDES = -I.
LFLAGS += -fpic -shared
LFLAGS += -lpthread
CFILES = a.c b.c c.c d.c e.c f.cpp g.cpp h.cpp i.cpp
##all: $(SFILES:.s=.s.o) $(CFILES:.c=.o) $(CFILES:.cpp=.o)
## $(CPP) $(INCLUDES) $(LFLAGS) -o libclient.so.1.0 $(CFILES:.c=.o) $(CFILES:.cpp=.o)
all: $(SFILES:.s=.s.o) $(CFILES:.c=.o)
$(CPP) $(INCLUDES) $(LFLAGS) -o libclient.so.1.0 $(CFILES:.c=.o)
%.o : %.c
$(CPP) -c $(CFLAGS) $< -o $@
%.o : %.cpp
$(CPP) -c $(CFLAGS) $< -o $@
clean:
rm *.o libclient.so.1.0
Your make file actually does work for c files, but not for c++ files.
This is because you invoke your custom compile command line for c compilation but your cpp files are going directly into what is effectively your link command line which does not specify compilation flags.
First make sure you're using the proper command, so copy paste the output from Makefile and execute that from your shell (by hand).
Maybe your define is undefined along the way so, here are two approaches to find out what's wrong:
1) include #warning statement(s) within your ifdef to see if it is really a missing define:
#warning "before the define"
#ifdef LINUX
#warning "here goes the linux define"
..inlcudes
#endif
if you compile the code and don't see the warning then indeed you miss the define somewhere.
2) Check the preprocessor output. To do so send the preprocessor (use cpp not g++) output to stdout by using the -E flag. (cpp -E ....)
By looking at output you can see all code included, so you can track down in detail what code your compiler gets. I find that method of last resort usually giving most insight into weird problems.
Change you ifdef
s to
#ifdef WIN32
...inclues
#endif
#ifdef __linux__
..inlcudes
#endif
If the ifdef
'ed content is UNIX rather than Linux specific use __unix__
You should be using a construct like this:
#ifdef _WIN32
// Windows specific #includes
#else
// Unix specific #includes
#endif
rather than setting yourself up to work only on Windows and Linux. The differences between Linux and other Unix systems are not worth worrying about till someone complains, but don't make life harder for yourself in advance.
精彩评论