|
|
Re: Struct... (Sdk)
Re: Struct... // Sdk
Aug 5, 1999, 9:12pm
In the AWCPP classes, there are no #includes in the .hpp files, although
they depend on definitions existing in other .hpp files. For example,
there's no #include Avatar.hpp in AvatarMe.hpp. When I try to compile
AvatarMe.C, I get compiler complaints. How can this be?
Tom Russell
[View Quote]Edward Sumerfield wrote in message <374BE48E.E4640884 at poboxes.com>...
>Your error is a link time error so the ifdef solution will not work. It is
>useful to prevent the same include file being included into a single source
>file multiple times but the #... statements are processed by the
>preprocessor which runs before the compiler do they have no baring on what
>the linker does.
>
>You need to understand externs to be able to solve your problem. Every
>variable defined outside a function is regarded as a global variable. These
>can only be defined once but can be referenced from anywhere. To be able to
>reference the variable from a different source file the linker must do the
>address resolution and that requires an "extern" definition in the other
>file.
>
>A.C
>int fred = 0;
>funcA {
> printf("fred = %d\n",fred);
>}
>
>B.C
>extern int fred;
>funcB {
> printf("fred = %d\n", fred);
>}
>
>You will notice that in A.C I have defined the global variable fred and
>initialized it. In B.C I have entered an extern references to it but have
>not initialized it. There is only one variable here, the extern is just a
>way to tell the linker to go look for a definition somewhere else, the
>extern is not really a variable, just a linker hint.
>
>Anyway, if you want to put your struct into a .h file and include that file
>into many source files then you will need to extern the variable that is
>your struct.
>
>It should be noted that it is not your struct that is giving you this
>problem but the variable that is of type struct. So lets say you have the
>following structure.
>
>struct foo {
> int a;
> int b;
>};
>
>This is a structure definition with no variable. I can follow it with a
>variable in two ways.
>
>struct foo {
> int a;
> int b;
>} foovariable;
>
>or
>
>struct foo foovariable;
>struct foo anotherfoovariable;
>
>In either case it is the definition of the foovarible that is giving your
>linker problems.
>
>I recommend the following solution.
>
>Do not use struct, instead use typedefs. They give you a cleaner definition
>and allow you to reference the type without having to specify struct
>everywhere.
>
>typedef struct {
> int a;
> int b;
>} MYFOO;
>
>This does not allocate any memory but just serves as a variable layout. It
>can be included into as many source files as your want and the linker will
>not complain.
>
>You can now define the variable by specifying MYFOO as a type like you
would
>int or char.
>
>MYFOO foovariable;
>
>Now you must add once as the real variable declaration and again as an
>extern. To do this use a #define as a "real declaration source file
>identification" flag.
>
>Storage.h
>typedef struct .....
>
>#ifdef REAL_DECLARATION
>MYFOO foovarible;
>#else
>extern MYFOO foovariable;
>#endif
>
>Now in one of your source files you can add the REAL_DECLARATION before the
>Storage.h include and all will be well.
>
>A.C
>#define REAL_DECLARATION
>#include <Storage.h>
>... // The variable will be real in this source file.
>
>B.C
>#include <Storage.h>
>.... // the variable will be extern in this source file.
>
>Hope this helps. Good luck.
>
>Edward Sumerfield.
>
>Andras Sarkozy wrote:
>
>
|
|