• Welcome to Valhalla Legends Archive.
 

VS.NET 2003 Preprocessor Include Problems

Started by Sargera, June 27, 2004, 07:24 PM

Previous topic - Next topic

Sargera

I've been developing an application recently that will be used for Battle.net (a moderation bot) and I've ran into a preprocessor include problem...

The problem:
I have two sets of files; Commands.cpp/Commands.h and Parse.cpp/Parse.h.  The command files contain various bot commands (ban, kick, etc.) and the parse files contain the connection code and the packet parsing code where commands are then called and processed in.  The problem I've ran into is that each file (the .cpp ones) require certian data structures.  Two structures, and about 8 maps.  The commands use them in their implementation code to check user flags and information.  The parse file uses them for testing purposes and other things that will be added later.  The point is, they each share the same set of maps/structs.  Now, I put the maps/structs in a file called Misc.h.  Commands.cpp and Parse.cpp each include Misc.h.  Everything works at compile-time.  At link-time however, errors occur complaining that the structs/maps already exist in Commands.obj.  I assume this is because both files include them and it's like a re-definition...How can I fix it to where both files have access to these set of maps/structs without causing these linker errors?

Zakath

Umm...first of all...you should be able to include something in multiple source files just fine. You ARE including a check in your source files to ensure that the header hasn't already been included, right?


#ifndef HEADERNAME_H
#define HEADERNAME_H

//code goes here

#endif


If for some reason that isn't working, try declaring the stuff as extern in one of the files. That certainly should fix it.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Sargera

Neither solution seems to work.  When externing the data in Misc.h it results in unresolved externals for each of the items externed.  When using the header file include checks it results in other unresolvex external symbol errors...

Adron

In the header files, put extern declarations like


extern somestruct variablename;




In one of the cpp files, put the actual variable definitions


somestruct variablename;



An alternative is to have a define set up like this:

header file:

...
#ifdef DEFINE_VARIABLES
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN somestruct variablename;


and then in one of the cpp files, you define DEFINE_VARIABLES before including the header file.