Thread: how to tell the compiler to avoid compiling sertion part ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    how to tell the compiler to avoid compiling sertion part ?

    Hello,..

    im working on applet that need to work both on windows and linux the problem is that i wish to create a single funciton that will work in diffrent ways on diffrent os .

    i thout using
    #ifdef __windows
    #include <foo4win>
    #endif __windows

    #ifdef __linux
    #include <foo4lin>
    #endif __linux

    int foo(){
    #ifdef __windows
    .
    .
    .
    #endif __windows

    ifdef __linux
    .
    .
    .
    #endif

    but i don't know what ifdef i should use (since __linux didn't compile).
    what is the proper #ifdef #endif for code for linux and windows
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I find that a good approach is to define different files for platform dependent code. You then instruct your build tool to build according to the platform.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you asking what the compiler provides as a prefdefined value for the OS? That is not strictly defined by any standard, but gcc is normally using the __linux__ form.

    In windows with a Microsoft compiler, you can use "_WIN32", but I'm not sure if that's supported by gcc.

    --
    Mats

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    thnx this is what im asking for ..

    ill check the __winr32
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include <blah blah.h>
    ...
    // Code that is platform independent...
    
    #if defined( WIN32 )
       // Put Windows specific code here.
    #elif defined( UNIX )
       // Put UNIX specific code here.
    #elif defined( SOMETHINGELSE )
       // Put code for other OS's in other #elif blocks...
    #endif

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    Thank to all .
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    To "fix" how different compilers define different constants for different OSes, use a couple of "ifs"

    eg,
    Code:
    #if defined(_WIN32) || defined(WIN32) || defined(__windows)
    #undef WIN32
    #define WIN32
    #endif
    Or whatever.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Many UNIX compilers define the preprocessor directive unix, though not in ANSI mode since in that case constants have to start with _X or __x.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. !!!Urgent Help on a group project!!!!!
    By AmeenR in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 09:22 PM