Author | Message | Time |
---|---|---|
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? | June 28, 2004, 12:24 AM |
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? [code] #ifndef HEADERNAME_H #define HEADERNAME_H //code goes here #endif [/code] If for some reason that isn't working, try declaring the stuff as extern in one of the files. That certainly should fix it. | June 28, 2004, 12:32 AM |
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... | June 28, 2004, 12:59 AM |
Adron | In the header files, put extern declarations like [code] extern somestruct variablename; [/code] In one of the cpp files, put the actual variable definitions [code] somestruct variablename; [/code] An alternative is to have a define set up like this: header file: [code] ... #ifdef DEFINE_VARIABLES #define EXTERN #else #define EXTERN extern #endif EXTERN somestruct variablename; [/code] and then in one of the cpp files, you define DEFINE_VARIABLES before including the header file. | June 30, 2004, 10:36 PM |