1

I work on a project which contains both c source files and c++ ones.
I use code like :

extern "C"
{
    #include "init.h"
    #include "ParameterGet.h"//ParameterGet.c exists
}

to include c files.

But I got a .h files of c which has no .c file. And it causes(as I think) many errors of :

multiple definition of `PeriodicFaultReport'
multiple definition of `FaultActiveEventReport'
multiple definition of `FaultInactiveEventReport'
multiple definition of `FaultLatchEventReport'

where PeriodicFaultReport,FaultActiveEventReport,FaultInactiveEventReport,FaultLatchEventReport are all declared in "DataStruct.h" and this file has no .c file with it.

Besides I notice that PeriodicFaultReport,FaultActiveEventReport,FaultInactiveEventReport,FaultLatchEventReport are all global variables.
[IMPORTANT]

Content relative in "DataStruct.h" of c:

struct  OptionalString PeriodicFaultReport;

struct  FaultActiveEventReportSet
{
    BOOL     Flag;
    UINT8    Type ;
    UINT32   Length;
    struct   Optional...  Report...[2];

}FaultActiveEventReport;

struct  FaultInactiveEventReportSet
{
    BOOL     Flag;
    UINT8    Type ;
    UINT32   Length  ;
    struct  Optional... Faultt...;

}FaultInactiveEventReport;

struct  FaultLatchEventReportSet
{
    BOOL     Flag;
    UINT8    Type ;
    UINT32   Length  ;
    struct  OptionalInteger  FaultID[2];

}FaultLatchEventReport; 

I know there may be an answer like here
But it is not right:
I get all the files from a c project, and there, "DataStruct.h" are included multiple times in other .h files, and it has no problem.
(The c project is in VS2008, which may be the reason of no problems)
But Is this true, VS helps to this and break the c standard?

Community
  • 1
  • 1
Al2O3
  • 3,103
  • 4
  • 26
  • 52
  • 4
    Are you sure those `struct`s weren't meant to be `typedef struct`s? – FatalError Mar 29 '13 at 02:07
  • @FatalError, no, surely. – Al2O3 Mar 29 '13 at 02:08
  • 5
    To explain FatalError's question - `struct X { ... } X;` defined a structure `X` and a variable/object of that type also called `X`... that's why including the header multiple times results in multiple definitions of the variable. If it were a `typedef`, it would meant that `X` can be used as a shorthand for `struct X`... legal in C++ anyway - can't remember for C. Separately, do you have any reason to think that this code's not broken? Someone might have written a program that only included it once, and not noticed the issue. To fix, make the vars in .h `extern` and create a DataStruct.c. – Tony Delroy Mar 29 '13 at 02:11
  • Thanks Lynn. Rubby - looking at your deleted answer - 'I get all the files from a c project, and there, "DataStruct.h" are included multiple times in other .h files, and it has no problem.' - as long as there are `#ifndef #endif` include guards or `#pragma once`, including multiple times will work if done from a *single* translation unit, but not from multiple translation units. (A translation unit is basically the code used to generate a single object output; the error would occur when linking multiple objects each defining the variables). – Tony Delroy Mar 29 '13 at 02:20
  • I'd all-but-guarantee FatalError is correct, especially if those are in a header file. Make sure your headers are guard-fenced as well. – WhozCraig Mar 29 '13 at 02:20
  • @WhozCraig, #ifndef __DATASTRUCT_H #define __DATASTRUCT_H#endif – Al2O3 Mar 29 '13 at 02:21
  • @Rubby Please update your code with those, so that we know you're applying them correctly... – autistic Mar 29 '13 at 04:41
  • Don't include C files in C++ projects. Compile them as C, and link the resulting object code to your C++ projects. There are numerous reasons for this, which I don't care to elaborate upon here. If you want answers, search for questions like "Why shouldn't I cast malloc?" and "Why does the `%` operator function differently in C++ than it does in C?". When configured to compile as C, VS2008s compiler still won't compile C99 code let alone C11 code. You are using a C++ compiler to compile C, but it can't compile valid C code written almost fifteen years ago. – autistic Mar 29 '13 at 04:44

0 Answers0