1

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
6
  • What compile commands are generated when you use make? Do they contain the -DLINUX and are they otherwise as you expect?
    – aschepler
    Feb 7, 2011 at 19:10
  • are you just looking for my output i get?
    – hrh
    Feb 7, 2011 at 19:23
  • I get lines like g++ -c -DLINUX -I. -c -g -O0 -Wall -fpic a.c -o a.o
    – hrh
    Feb 7, 2011 at 19:23
  • I should also Note that this is my first time working with Linux and make files, so if there is something obvious i'm missing let me know.
    – hrh
    Feb 7, 2011 at 19:37
  • 3
    Your life will be a lot easier if you follow standard conventions. For example, if you use LDFLAGS and CXXFLAGS, you can allow the default rules to work for you. In the future, you will run into a lot of problems when you discover that the default rules will use CPP as the c preprocessor, and CXX as the C++ compiler. Feb 7, 2011 at 20:09

4 Answers 4

3

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.

3

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.

5
  • so I added the warnings and it looks like it IS getting into the defines, however it still doesn't work unless I comment #ifdef & #endif, im not sure where or how it can be getting unincluded or undefined.
    – hrh
    Feb 8, 2011 at 15:01
  • Actually it seems that at some points it IS getting in and some points its NOT.
    – hrh
    Feb 8, 2011 at 15:02
  • Figured out the multiple definition problem. During the linking process I was accidently including both the .o files and the .c files, I fixed it so that it is only including the .o files and now it complies correctly.
    – hrh
    Feb 8, 2011 at 15:41
  • The -DLINUX does not affect the linking process. The problem must still be at the parsing(preprocessor) phase. It seems that there is multiple inclusion of header files (forgot guards around the included header files ?) which undefine or define the LINUX. The best would still be to check the preprocessor output. But well, it seems you got it under control.
    – count0
    Feb 8, 2011 at 15:45
  • I figured out the problem and Chris was correct, I was also linking my cpp files, when it should of been just the .o files
    – hrh
    Feb 8, 2011 at 16:01
1

Change you ifdefs to

#ifdef WIN32
...inclues
#endif
#ifdef __linux__
..inlcudes
#endif

If the ifdef'ed content is UNIX rather than Linux specific use __unix__

0

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.

5
  • Possibly helpful, but not an answer to the question.
    – aschepler
    Feb 7, 2011 at 20:12
  • 1
    @aschelper The question doesn't actually ask anything. It states a problem and then posts a makefile. This answer (and mine) attempt to bypass a lot of irrelevant BS and answer the question 'How can I have linux-specific code in my project?' rather than the assumed (by you) question of 'How can I define LINUX in my project (so that I can have linux-specific code in my project)'. Note that outside of the makefile, not a single '?' is used in the question
    – KitsuneYMG
    Feb 7, 2011 at 20:51
  • This method will include Unix-specific code even if compiled under something other than Windows or Unix.
    – Blrfl
    Feb 7, 2011 at 22:25
  • To first order Windows and Unix are the only remaining games in town. Port to VMS or z/OS or one of the increasingly rare non-POSIX embedded operating systems only when you have actual user demand, not before.
    – zwol
    Feb 7, 2011 at 22:35
  • Actually windows and unix are far from being the only games. I've had a lot of code that built for Window, Linux, and some small embedded platform. Later on, embedded linux became a 4th target distinct in some key (available I/O) respects from the desktop version. Feb 8, 2011 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.