Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use extern to declare an enum type

Tags:

c++

enums

Now I have two VC projects: A and B. I'm working with Project B, I need to use an enum type defined in a header file in Project A, I cannot include this header file.

Can I use extern to extend the visibility of the enum type to Project B?

If so, how can I do it? And if not, is there any other way to use this enum type in Project B?

like image 243
pangshapeng Avatar asked Feb 14 '23 16:02

pangshapeng


2 Answers

Can I use extern to extend the visibility of the enum type to Project B?

No. The definition of an enumeration is needed in any translation unit that uses it.

Is there any other ways to use this enum type in Project B?

No. Your only options are to make the header in A available to B, or copy the definition and find some way to keep the copies consistent.

like image 160
Mike Seymour Avatar answered Feb 17 '23 05:02

Mike Seymour


extern only tells the compiler that the definition for a particular symbol is some other file the current one you only have the declaration. This the mechanism you use to make global variable visible in your source code. So for your case, extern will not do.

what you can do is have a common header file for both project and in there:

typedef enum{
...
//enum members
...
};

This way both project can use the same enum.

like image 38
Pandrei Avatar answered Feb 17 '23 07:02

Pandrei