Thread: Global #define w/C Preprocessor

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Global #define w/C Preprocessor

    // File1.c
    Code:
    #include "File1.h"
    #include "File3.h"
    
    int main( void )
    {
      myFunc();
      return 0;
    }
    // File1.h
    Code:
    #define _FILE1_
    #ifndef _FILE1_H_
    #define _FILE1_H_
    
    int main( void );
    
    #endif
    // File2.c
    Code:
    // same as File1.c cept replace FILE1 w/ FILE2
    // File2.h
    Code:
    // same as File1.h cept replace FILE1 w/ FILE2
    // File3.c
    Code:
    #include <stdio.h>
    #include "File3.h"
    
    int myFunc( void )
    {
      printf( "%s", MY_DEFINED_STRING );
      return 0;
    }
    // File3.h
    Code:
    #ifndef _FILE3_H_
    #define _FILE3_H_
    
    #if defined (_FILE1_)
    #define MY_DEFINED_STRING "File1"
    #elif defined (_FILE2_)
    #define MY_DEFINED_STRING "File2"
    #else
    #define MY_DEFINED_STRING "Unkown"
    #endif
    
    int myFunc( void );
    
    #endif
    Trying to get something like the above to work. It's compiles and runs on my compiler but my end result is always "Unknown". Is there a way to make a global #define?
    Last edited by TheUaRT; 12-08-2006 at 12:07 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You shouldn't have int main()s in your header files. . . That will seriously confuse your linker.

    Anything you #define in a header is global to anything that #includes it.

    EDIT: Also, use code tags around your code. Break up the individual files with separate sets of code tags. . . what you wrote is really hard to read.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you mean 'work'?

    It works as is, but without a clear definition of what you expect to see, then it's hard to say how you should fix it.
    Also, what are you trying to achieve, maybe there's a completely different approach.

    As it stands, file3.c doesn't include file1.h or file2.h, so neither of those can have any effect on the compilation of file3.c
    You would at least need to include another header in file3.c (before file3.h is included) in order to make it recognise the define.

    Now you could do this
    gcc -D_FILE1_H_ file3.c
    but that's just going to muck up the compilation of other modules.

    In some sense, that is a 'global' define since each compilation unit will see that define if that's how you want to arrange your build.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    Quote Originally Posted by Salem
    Now you could do this
    gcc -D_FILE1_H_ file3.c
    but that's just going to muck up the compilation of other modules.
    Thanks.. the gcc -D option works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  3. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 04:55 PM
  4. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM