Thread: How to make a #define global

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    How to make a #define global

    Ok i have a problem, I want to do some conditional compilation using some #define's but I have to do them in every file that uses this conditional compiling and its anoying, even if i put it into a header file and then include that header somewhere else the #define is not recognized in the other file, ex:

    Code:
    //file Test1.h
    #define USERDEF 1
    
    //file Test2.h
    #include"Test1.h"
    
    int SomeFunc()
    {#ifdef USERDEF
     // code here
     #elif !defined USERDEF
     // other code here
     return(something);
    }
    When I try this MSVC keeps complaining about USERDEF being undefined and this i really dont want to have to do the #define in all the files that use the conditional compilation, any way around this b/c im pretty new to messing w/ the preprocessor.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A #define is global to all files that include the header or include headers that include the header. Double check your spelling, the problem is probably a syntax error somewhere.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    Review the article under: file scope, regarding scope and visibility in the MSDN lirary. It's online at microsoft. Or you could just get a programming text book and read up about it. Otherwise, you must incude it in every file that defines your constants and functions.

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Well its not a spelling error lol, first i just typed it in the second file and it said that so then i was like ok and did a copy and paste and it still says USERDEF is still underfined. So is the only way to define this in every header that uses this? Ive changed this to using constants but now its not conditional compilation so instead it has to pick the correct method at runtime which is something i wanted to avoid but oh well, thanx for the help anyways guys.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    TheG3
    Guest

    space maybe?

    Code:
    #include"Test1.h"
    
    #include "Test1.h"
    Missing space maybe? Not sure on syntax on that though...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. 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
  5. Stiff sets of equations
    By Mistert77 in forum C Programming
    Replies: 7
    Last Post: 10-27-2003, 12:36 PM