Thread: Include depending on system.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Include depending on system.

    Hey guys.

    I seem to have a small problem.
    I want to make a condition in the c preprocessor, that if it is a specific system I need to do something.

    I have FreeBSD and SunOS. I need it to do:

    #if this_system_is_SUN
    #define IS_SUN 1
    #else
    #define IS_SUN 0
    #endif

    I am a noob when it comes to c preprocessing.

    2nd problem:
    How can I specify this also for the makefile?
    If its SUN I need it to execute /usr/sfw/bin/gcc and if it is FreeBSD I need it to be just gcc.
    Thank you!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are no pre-defined defines or such that's specific and standard for which OS you use, so the best is simply to define one yourself. If compiling for FreeBSD, then just do #define FreeBSD or if it's SunOS do #define SunOS.
    Then you can check

    #ifdef FreeBSD
    /* Something here */
    #elif defined(SunOS)
    /* Something here */
    #endif
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    There are no pre-defined defines or such that's specific and standard for which OS you use
    Yeah there are. Salem posted a huge list of them a few weeks ago. But better to do it explicitly anyway.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not thinking they are standard? Or maybe I remember wrong.
    Hmmm. I'll go have a look.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    I'm not thinking they are standard? Or maybe I remember wrong.
    Hmmm. I'll go have a look.
    Why does it matter if they are standard? Attempting to figure out your platform is a non-standard activity to begin with. They are consistent and dependable if that's your question.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because if they're not standard, you must advise that they aren't standard and might not work across all compilers, so you should know what you're doing and getting yourself into
    Anyway, have a look: http://predef.sourceforge.net/preos.html
    Many defines for platforms there. Hope they work!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Because if they're not standard, you must advise that they aren't standard and might not work across all compilers, so you should know what you're doing and getting yourself into
    A compiler that doesn't support the typical platform-specific defines is a toy and should not be used.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No wonder compilers hate you
    Nothing says the defines need to be the same across all compilers, just like all #pragma's aren't the same.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you do it that way, you don't. You explicitly specify what platform you compile for.
    Otherwise you can try these http://predef.sourceforge.net/preos.html.
    Just check with #ifdef if it's defined and it should work without you explicitly specifying what platform to compile for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    No wonder compilers hate you
    Nothing says the defines need to be the same across all compilers, just like all #pragma's aren't the same.
    Have you ever actually worked on a large variety of systems?

    The native compiler sets the standard. For instance, the Sun compiler which comes with Solaris defines a number of macros. This sets the standard. gcc and other compilers which are ported to this platform provide the same symbols. To not do so it to be blatantly incompatible with the platform. For one thing it would make it impossible to use the native C library because the headers assume these macros are defined properly. A compiler which makes it impossible to use the native libraries is a toy.

    Similarly HP's compiler sets the standard for HPUX, IBM's AIX compiler sets the standard for AIX, etc. Suppose you're working on some embedded test board which comes with an industry-standard compiler. THAT compiler is the standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  2. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  3. Failed to get POLLHUP event
    By shugbg in forum Linux Programming
    Replies: 5
    Last Post: 04-27-2007, 10:00 AM
  4. Socket programming
    By kahad in forum C Programming
    Replies: 3
    Last Post: 12-14-2006, 04:37 PM
  5. Replies: 3
    Last Post: 06-13-2005, 07:28 AM