Thread: extern with enum

  1. #1
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    extern with enum

    I created an enum in a file as follows:

    Code:
    enum {
                SOME_VAL=201,
                ANOTHER = 202
    };
    I then want to use those values in another file that I am linking together with this file. My question is how to I declare the enum so that the SOME_VAL and ANOTHER retain there values and not reset to 0 and 1 respectively. Currently, I had the following in my other file:
    Code:
    extern enum {
                SOME_VAL,
                ANOTHER
    };
    and this did not work.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't. Put them in a header file and include it. Since it looks like you're not declaring an enum type variable, rather, you're just using them for their numeric placeholder, a header would work just fine.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    My intent is to have one file which is shared across many applications. Ideally, I would like that file to reference the names and values within the enum which are defined in whichever program happens to be linked with the file. I don't want to have to have different headers containing different values in the enum whenever the program changes for example:

    If prog1 contains

    Code:
    enum {
       a=1,
       b=2
    };
    if prog2 contains:
    Code:
    enum {
        a=5,
        b=10
    };
    I want common file to contain a reference to a and b where a and b will be set to whatever value the file that the common file is being linked with. If common is linked with prog1 then I want common a and b to be 1 and 2. If prog2 is linked with common then I want common a and b to be 5, and 10.

    I originally had the enum as a type but had no idea how to accomplish what I just described above using my own type.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't see the point, but I don't see the problem either. Make two headers, and compile using whatever one you want. Or make a single header, and compile with whatever portion you want, like so:
    Code:
    #ifndef MY_HEADER
    #define MY_HEADER
    
    #ifdef USE_1
    enum { value_a = 1, value_b = 2 };
    #else
    enum { value_a = 10, value_b = 20 };
    #endif
    
    #endif
    Use one header. Compile with "-DUSE_1" or without.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    The problem is that the enums are already existing in the other programs and they are a much larger structure than the one I provided. Ideally, I didn't want to have to move anything and would like the common file to just reference the appropriate external enum values that are already defined. I get the sense that there is no way to do this - that I'll have to implore a method similar to what you are showing with a precomp within a header.

    linking common to prog1 and prog1 already has large enum def
    linking common to prog2 and prog2 already has large enum def
    linking common to prog3 and prog2 already has large enum def
    linking common to prog4 and prog2 already has large enum def
    linking common to prog5 and prog2 already has large enum def
    .
    .
    .

    Thanks anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  2. need help with extern pointers.
    By broli86 in forum C Programming
    Replies: 17
    Last Post: 04-11-2008, 09:16 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM