Thread: Preprocessor symbols

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    13

    Preprocessor symbols

    I am writing some code that is to be protable across linux, unix and windows. I need to exclude some lines when compiling for windows. I have discovered that the symbol __unix__ may be helpful.

    What other symbols are standard or common?

    Is there a __windows__ or __win2000__?

    Where might I find a listing of them for future reference?

    When they are "defined", do they have a value or are they like a boolean?

    ThanX in advance

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    I tried __WIN32__ as well as __win32__ and __Win32__. None of them print anything from the following code snippet.

    #ifdef __win32__
    printf("__win32__\n");
    #endif
    #ifdef __Win32__
    printf("__Win32__\n");
    #endif
    #ifdef __WIN32__
    printf("__WIN32__\n");
    #endif

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Is there a way to get the compiler to tell all defined symbols, macros, etc?

    I am using VC++ v6.0 at the command prompt on the PC, gcc v3 on my solaris machine, gcc on a red hat linux 8.0 machine and gcc v3 on a Mac OS X machine. I basically need to automatically differentiate between all of the above.

    I just tried _WIN32 and the compiler on the PC freaked out. Something about a "missing" ";". If I remove the printf commands from

    #ifdef _WIN32
    printf("Win\n");
    #endif
    and it compiles. Apparently I struck a nerve.

    I searched MSDN but I seemed to have gotten a whole lot of nothing. Am I using the wrong words to describe what I'm looking for? Am I asking too much?

    thanX again

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Now I've gotten _WIN32 to work, but only if I moved it to a different location in the code!?!?!?!

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    I think that I've got a basic handle on the current problem. I had these commands before the variable declarations. Apparently that made the Visual C++ compiler angry. It complained about a missing ; before struct. I've included the first several lines of compiler errors that are produced when the #ifdef's are before the variable declarations.

    I just tried the included snippet with the #ifdefs before and after the variables and it crashes too. Does Windows require variable declaration come first?

    There is a lot of code, so I can't really show it all, but the important stuff is below.

    Current code follows and so far distinguishes between my Win2000 machine, my Sun Solaris machine, and my Red Hat 7.3 (Intel) machine. My final test will be my Mac running OSX.

    int main(int argc, char *argv[])
    {
    struct tm *currenttime;
    time_t caltime;
    FILE *fp;
    short int naxis;

    #ifdef __linux__
    printf("Running on a Linux Machine.\n");
    #endif
    #ifdef _WIN32
    printf("Running on a Windows Machine.\n");
    #endif
    #if defined(__unix__) && !defined(__linux__)
    printf("Running on a Unix Machine.\n");
    #endif
    #if !defined(_WIN32) && !defined(__unix__) && !defined(__linux__)
    printf("Don't know what you're running on!\nThere may be problems.\n");
    #endif

    }
    Windows compiler errors(first part)

    makefits.c
    makefits.c(48) : error C2143: syntax error : missing ';' before 'type'
    makefits.c(49) : error C2275: 'time_t' : illegal use of this type as an expression
    C:\PROGRA~1\MICROS~2\VC98\INCLUDE\time.h(79) : see declaration of 'time_t'
    makefits.c(49) : error C2146: syntax error : missing ';' before identifier 'caltime'
    makefits.c(49) : error C2065: 'caltime' : undeclared identifier
    makefits.c(50) : error C2143: syntax error : missing ';' before 'type'
    makefits.c(51) : error C2275: 'FILE' : illegal use of this type as an expression
    C:\PROGRA~1\MICROS~2\VC98\INCLUDE\stdio.h(156) : see declaration of 'FILE'
    makefits.c(51) : error C2065: 'fp' : undeclared identifier
    makefits.c(52) : error C2143: syntax error : missing ';' before 'type'
    makefits.c(69) : error C2065: 'currenttime' : undeclared identifier
    makefits.c(69) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct tm *'
    makefits.c(79) : error C2065: 'filename' : undeclared identifier
    makefits.c(79) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
    makefits.c(79) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
    makefits.c(80) : error C2100: illegal indirection

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Thank you for helpin' me out. One more question on this topic though. Is there a way to get the compiler to tell you what is currently "DEFINED"?

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    As described in the gcc man page the following example works great. (it assumes there is no foo.h)

    touch foo.h ; cpp -dM foo.h

    I looked at the help page and cl /? output, but couldn't find the equivalent for MSVC++ command line compiler. Do you know if there exists the same command for MSVC++'s cl?

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Oh well.

    thanX again.

Popular pages Recent additions subscribe to a feed