A
A
Atilla2013-09-13 23:19:46
C++ / C#
Atilla, 2013-09-13 23:19:46

One-time structure declaration in the project

There are, let's say, three files:
blah.c blah.h main.c
in blah.hthe structure is declared:
structure blah test_blah (...);
in blah.cit is described (and the file is included blah.h).

In main.c is included blah.h

In this case, when trying to compile these files, the compiler complains about the duplicate declaration of the structure test_blah.
Therefore, I write instead structure blah test_blah (...);
static structure blah test_blah (...);

of But in this case, 2 independent instances are created test_blah(in blah.cand main.c), which is not desirable.

How to make it so that only 1 instance is declared test_blahinmain.c

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
MikhailEdoshin, 2013-09-13
@Atilla

You are creating a variable, not just declaring a structure. test_blahis a variable, data type struct blah. It was probably thought like this:

/* blah.h */
struct blah; /* урезанное описание */

/* blah.c */
#include "blah.h"
struct blah { int a, b } /* описание */
test_blah; /* и тут же объявление переменной этого типа */

/* main.c */
#include "blah.h"
/* можно работать с указателями на struct blah */

D
Door, 2013-09-14
@Door

Not "description" and "description", but declaration ( declaration ) and definition ( definition ).
If you have a type ( struct blah) and you need one instance ( instance ) (in your case, test_blah ) of this type, which you need to have access to from anywhere in the program, then you need to define this instance once (of course, you won’t be able to , ODR Define - write the full name of the type, specifying the name of the variable in one translation unit), and before using declare, previously indicating that the variable (instance) is "external" ( extern) in relation to the current translation unit (usually, this is a separate .cpp file after the preprocessor).
As a result, you have, roughly speaking, 2 translation units ( blah.c , main.c ). You need to define a variable in any of them (at the point, of course, when the type of the variable is defined, not declared). And, for convenience (so that later you can just include the header file, and not write the same thing every time), declare a variable in blah.h , and by including blah.h in any desired .cpp file, use it. Those.:

  1. // blah.h
  2.  
  3. #ifndef BLAH_H
  4. #defineBLAH_H
  5.  
  6. // We declare and define the type (you can only declare it, but define it elsewhere)
  7. struct  blah
  8. {
  9.     int  value ;
  10. } ;
  11.  
  12. // Declare a variable, indicating that it is defined elsewhere
  13. // (Because at this point we have a complete type definition,
  14. // you can declare blah_test of type struct blah, and if the definition
  15. // was in a different place (i.e. would just have struct blah; above), then you would have to
  16. // declare a pointer to struct blah, i.e. struct blah*)
  17.  
  18. extern  struct  blah blah_test ;
  19.  
  20. #endif
  21.  
  22. // ---------------------------------
  23. // blah.cpp (blah.c)
  24.  
  25.  
  26. #include "blah.h"
  27.  
  28. // Define a variable
  29. struct  blah blah_test ;
  30.  
  31. // ---------------------------------
  32. // main.cpp (main.c)
  33.  
  34. // Including the header file, we declare a variable,
  35. // which we want to use.
  36. #include "blah.h"
  37.  
  38. int  main ( )
  39. {
  40.     // Use
  41.     blah_test. value  =  0 ;
  42.  
  43.     return  0 ;
  44. }
  45.  
  46.  

A
AxisPod, 2013-09-14
@AxisPod

In bare C, use extern, in C++ use singleton.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question