Thread: Preproccessor conditional compilation

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Preproccessor conditional compilation

    I have a question about compiliation with the preproccessor:
    Code:
    #ifdef __UNIX__
    /*...etc... */
    What do you compile differently and all that if it's Unix? You know what machine you are compiling on, so what is conditional compilation used for? Isn't code compiled into machine code and linked? Then why would you need conditional compilation? Thanks...

    --Garfield
    1978 Silver Anniversary Corvette

  2. #2
    Unregistered
    Guest
    Portability. There are many flavours of *nix. They do not all function exactly the same. Additionally:
    Code:
    #ifdef DEBUG
       perror("Error writing to file...blah blah");
    #endif
    Things like that are useful also. Why remove all your debug code from the source code just to put it back in when you need it?

    Quzah.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Re: Preproccessor conditional compilation

    Originally posted by Garfield
    I have a question about compiliation with the preproccessor:
    Code:
    #ifdef __UNIX__
    /*...etc... */
    What do you compile differently and all that if it's Unix? You know what machine you are compiling on, so what is conditional compilation used for? Isn't code compiled into machine code and linked? Then why would you need conditional compilation? Thanks...

    --Garfield
    Because, you might want to write stuff that will work on more than one system. So, you want it to compile in Windows, Linux, all flavors of unix, etc.

    trust me, in the Unix world, porting code cross-platforms is a daily occurance for many people. This makes their lives easier.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Because, you might want to write stuff that will work on more than one system. So, you want it to compile in Windows, Linux, all flavors of unix, etc.
    So, you're saying that when it compiles, it checks to see if you are compiling on a Unix machine (in this example)? So, if you write code that you know you are going to compile on a Unix machine and you don't want to distribute the source code, then this code would be unneccessary, right? Because you know what machine you are compiling on. Thanks.
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    2
    Also for testing..

    For example, right now I'm working on project, and I must know how much time each routines takes.
    So I've created the timing measurement code and include it only if I define some macro.
    #ifdef TIMING_ON
    Start_timing_routine();
    #endif

    After I measure it all, I won't need this code, so instead of removing it from entire project, I will do onlu
    #undef TIMING_ON
    and the code will be free of it..

    Also if you have different variants of your application (shareware, trial, full....) then you will use the compiler switches.

    Hope it helps
    Regards

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Garfield

    So, you're saying that when it compiles, it checks to see if you are compiling on a Unix machine (in this example)? So, if you write code that you know you are going to compile on a Unix machine and you don't want to distribute the source code, then this code would be unneccessary, right? Because you know what machine you are compiling on. Thanks.
    Yup. How you code is determined, in a large part, by what you're coding.

    Example: I have been releasing various freeware apps, mostly editors for video games, for years. Looking at one recent example, my code is highly-nonportable -- I make use of all kinds of compiler-specific features, windows-specific features, etc. This makes my program quicker and easier to write. I have no intention of ever releasing source, and as the games only work on a windows platform, the program would be useless to port.

    So, for this example, it only makes sense to release it only as a binary. Then, so long as it compiles on my compiler, nothing else matters.

    Now, say I was making a library of functions like the STL. Such an application would require the source code to be highly portable, so you won't know what kind of machine you're compiling on, so you can't make any assumptions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PC RAM in Compilation
    By SlyMaelstrom in forum Tech Board
    Replies: 5
    Last Post: 05-21-2006, 06:03 AM
  2. Inconsistent Compilation
    By GlassEyeSlim in forum Linux Programming
    Replies: 2
    Last Post: 02-23-2006, 06:43 PM
  3. Compilation units
    By filler_bunny in forum C++ Programming
    Replies: 0
    Last Post: 10-21-2003, 03:57 AM
  4. MS VC++ Crash on compilation
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-23-2003, 07:06 PM
  5. malloc problem in SUN in 64-bit compilation
    By ylzhang in forum C Programming
    Replies: 6
    Last Post: 05-31-2003, 11:48 AM