0

So I have several autogenerated files, each defines a complex struct with a unique name. They consists of some primitive types and structs.

Here is a little scheme of that two files. One car.h and one bike.h, each file defines the same struct called Coordinates. But when I try to compile I get redefinition errors.

Is it possible to include that header files both? Or do I have to merge them into one header?

car.h
#ifndef CAR_H_
#define CAR_H_

#pragma pack(push,4)
typedef struct
{
    uint32 pos_x;
    uint32 pos_y
} Coordinates;
#pragma pack(pop)

#pragma pack(push,1)
typedef struct
{
    tUInt8 id;
    Coodinates position_of_car;
} Car;
#pragma pack(pop)
[...]

Bike.h defines Coordinates as well.

bike.h
#ifndef BIKE_H_
#define BIKE_H_

#pragma pack(push,4)
typedef struct
{
    uint32 pos_x;
    uint32 pos_y
} Coordinates;
#pragma pack(pop)

#pragma pack(push,1)
typedef struct
{
    uint8 id;
    Coodinates position_of_car;
} Bike;
#pragma pack(pop)
[...]

Thank you for your suggestions in advance!

CanO
  • 111
  • 1
  • 4
  • Not your problem, but you have tagged this C++. `typedef struct { ... } Foo` would be much better as `struct Foo { ... };` (Even in C you can, and probably should, write `typedef struct Foo { ... } Foo;` - that way you can refer to `struct Foo` without the #include.) – Martin Bonner supports Monica Nov 21 '16 at 08:49
  • May be that's more a problem how the generator is fed. I suspect you use some kind of data structure scheme and defined `Coordinates` locally for `Car` and `Bike`, while this structure should be shared by both. – πάντα ῥεῖ Nov 21 '16 at 08:54
  • The generator is fed with one header. I am just selecting `Car` and `Bike` to extract them into an own header with their dependancies. – CanO Dec 05 '16 at 08:36

1 Answers1

0

This is likely to cause a redefinition error. I would advise to put the Coordinates structure in its own header file (with protection against multiple inclusion) and include it from Car.h and Bike.h

MrPromethee
  • 721
  • 9
  • 18
  • 1
    The files are auto-generated. It is not clear whether OP can just put the structures in their own header. – Micha Wiedenmann Nov 21 '16 at 11:18
  • Putting them in an own header would be possible. But the two structs Car and Bike are just for illustration. The productive headers and structs are more complex and I would not benefit with this solution. – CanO Dec 05 '16 at 08:33