enum used in multiple compilation units

  • Thread starter =?ISO-8859-1?Q?Jens_M=FCller?=
  • Start date
?

=?ISO-8859-1?Q?Jens_M=FCller?=

I have a file here with several enums:

#ifndef PLANARSEP_OPTIMIZE_H
#define PLANARSEP_OPTIMIZE_H


enum fund_cycle_behavior_t
{PASS_MODE_FIRST,
PASS_MODE_BEST,
PASS_MODE_ALL};

enum optimization_criterium_t
{OPTIMIZE_NOTHING,
OPTIMIZE_SEPARATOR,
OPTIMIZE_BALANCE,
OPTIMIZE_RATIO};


enum end_opt_t
{END_OPTIMIZE_NOTHING,
END_OPTIMIZE_EXPEL,
END_OPTIMIZE_DUL_MEN,
END_OPTIMIZE_EXPEL_DUL_MEN,
END_OPTIMIZE_DUL_MEN_EXPEL};

#endif

Before, the enums didn't have a name and everything worked.

Now that I included the names (in order to store the values in variables
having the enum type), I get the following error when linking:

Link separator
libseparator_time.a(g_planar_separator_time.o):(.bss+0x0): multiple
definition of `fund_cycle_behavior_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x0): first defined here
libseparator_time.a(g_planar_separator_time.o):(.bss+0x4): multiple
definition of `optimization_criterium_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x4): first defined here
libseparator_time.a(g_planar_separator_time.o):(.bss+0x8): multiple
definition of `end_opt_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x8): first defined here
libseparator_time.a(complex_separation_time.o):(.bss+0x0): multiple
definition of `fund_cycle_behavior_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x0): first defined here
libseparator_time.a(complex_separation_time.o):(.bss+0x4): multiple
definition of `optimization_criterium_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x4): first defined here
libseparator_time.a(complex_separation_time.o):(.bss+0x8): multiple
definition of `end_opt_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x8): first defined here
collect2: ld returned 1 exit status

I haven't changed any other code yet ...

Can't I declare an enum in a header file included in multiple
compilation units?
 
M

mlimber

Jens said:
I have a file here with several enums:

#ifndef PLANARSEP_OPTIMIZE_H
#define PLANARSEP_OPTIMIZE_H


enum fund_cycle_behavior_t
{PASS_MODE_FIRST,
PASS_MODE_BEST,
PASS_MODE_ALL};

enum optimization_criterium_t
{OPTIMIZE_NOTHING,
OPTIMIZE_SEPARATOR,
OPTIMIZE_BALANCE,
OPTIMIZE_RATIO};


enum end_opt_t
{END_OPTIMIZE_NOTHING,
END_OPTIMIZE_EXPEL,
END_OPTIMIZE_DUL_MEN,
END_OPTIMIZE_EXPEL_DUL_MEN,
END_OPTIMIZE_DUL_MEN_EXPEL};

#endif

Before, the enums didn't have a name and everything worked.

Now that I included the names (in order to store the values in variables
having the enum type), I get the following error when linking:

Link separator
libseparator_time.a(g_planar_separator_time.o):(.bss+0x0): multiple
definition of `fund_cycle_behavior_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x0): first defined here
libseparator_time.a(g_planar_separator_time.o):(.bss+0x4): multiple
definition of `optimization_criterium_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x4): first defined here
libseparator_time.a(g_planar_separator_time.o):(.bss+0x8): multiple
definition of `end_opt_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x8): first defined here
libseparator_time.a(complex_separation_time.o):(.bss+0x0): multiple
definition of `fund_cycle_behavior_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x0): first defined here
libseparator_time.a(complex_separation_time.o):(.bss+0x4): multiple
definition of `optimization_criterium_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x4): first defined here
libseparator_time.a(complex_separation_time.o):(.bss+0x8): multiple
definition of `end_opt_t'
libseparator_time.a(PlanarSeparator_time.o):(.bss+0x8): first defined here
collect2: ld returned 1 exit status

I haven't changed any other code yet ...

Can't I declare an enum in a header file included in multiple
compilation units?

Yes, you can. Please consult
http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8 in posting
code that doesn't work. The summary: provide a minimal but *complete*
example that we can cut and paste unchanged into our editors that
demonstrates your problem.

Cheers! --M
 
V

Victor Bazarov

Jens said:
I have a file here with several enums:

[...]

Before, the enums didn't have a name and everything worked.

Now that I included the names (in order to store the values in
variables having the enum type), I get the following error when
linking:
Link separator
libseparator_time.a(g_planar_separator_time.o):(.bss+0x0): multiple
definition of `fund_cycle_behavior_t'
[..]

I haven't changed any other code yet ...

Can't I declare an enum in a header file included in multiple
compilation units?

Should be able to, an 'enum' declaration only defines a unique type.

You need to consult your compiler documentation. Apparently it
thinks your enums are objects or something, and we can't really
help you, linking errors are not defined in C++. Are you sure the
code you posted is what your code looks like? Do you think you
could follow the recommendations of FAQ 5.8?

V
 
?

=?ISO-8859-1?Q?Jens_M=FCller?=

Victor said:
Should be able to, an 'enum' declaration only defines a unique type.

OK, the fact that it _should_ work is enough for the moment. Tried a
simple test program with a common enum type in both of two object files,
everything works as expected.


You need to consult your compiler documentation. Apparently it
thinks your enums are objects or something, and we can't really
help you, linking errors are not defined in C++. Are you sure the
code you posted is what your code looks like?
Do you think you
could follow the recommendations of FAQ 5.8?

Sorry, not currently, it's a not very small and quite bad-structured
project I am beginning to dive in.

I have just added a name to one of the enum types, and it worked. Second
one, still worked. Third one - works.

WTF?! Sorry to have bothered you ...

Only explanation I have at the moment: Wrong dependencies and object
code lying around having an old definition ... But I really don't know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top