![]() |
|
Multiple source files and headers |
moon_rabbits
Member #8,469
March 2007
![]() |
I have two .cpp files and one .h file in my current project, here's the source: main.cpp
menu.cpp
dialog.h
It SHOULD compile right, right? Except I'm getting errors that say all of the menus and the_dialog are being declared multiple times. Shouldn't the lines: #ifndef DIALOG_INF_H #define DIALOG_INF_H prevent this? |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote: all of the menus and the_dialog are being declared multiple times. They have to be declared in a .cpp file. Include guards can guard against some things, but not this. |
moon_rabbits
Member #8,469
March 2007
![]() |
How do I do that, though? Now I have: dialog.h and the dialog.cpp file:
The other two .cpp files (menu.cpp and main.cpp) are unmodified, but I still get the same errors. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Add the word 'extern' to the variable definitions in your header files.
|
Kibiz0r
Member #6,203
September 2005
![]() |
Doesn't that also mean adding the type identifier to the .cpp file?
--- |
LennyLen
Member #5,313
December 2004
![]() |
Quote: The declaration is made in one of the .cpp files, and all the others include the .h file that has the extern definitions.
|
Kitty Cat
Member #2,815
October 2002
![]() |
LennyLen said:
Quote:
extern MENU file_child[5]; // incorrect
extern MENU file_child[]; // correct The former is perfectly correct (as is the latter). Putting the values like that even helps the compiler, in some situations. That said, you generally don't put the size of the array in MENU or DIALOG arrays, since they're NULL terminated anyway, it makes increasing or decreasing the size easier, and the benefits aren't too useful when dealing with them. There's nothing wrong with it of course, it's just not something I see done. -- |
LennyLen
Member #5,313
December 2004
![]() |
Quote: The former is perfectly correct (as is the latter). Putting the values like that even helps the compiler, in some situations. Oh? I thought it counted as a multiple declaration if more than one file includes that header?
|
Kitty Cat
Member #2,815
October 2002
![]() |
Not as long as it's declared extern. Putting in the array size just lets the compiler know how big the array is (without it, things like sizeof(file_child) won't work). -- |
CGamesPlay
Member #2,559
July 2002
![]() |
I wrote about this about a week ago... Read up! -- Ryan Patterson - <http://cgamesplay.com/> |
moon_rabbits
Member #8,469
March 2007
![]() |
I'm still a little bit confused, as I've never used extern before. I get it, but it's still not working. main.cpp has remained unchanged. dialog.h is:
and dialog.cpp is:
And yet I am still getting multiple definition errors. Also, should I include allegro.h and agup.h in all the .cpp files, or just in the dialog.h, since dialog.h is included in all the .cpp files, doesn't that mean allegro is too? Or would it be better form to just include it in all the files it's used in, even if it was already included by dialog.h? |
ImLeftFooted
Member #3,935
October 2003
![]() |
You can include allegro.h and agup.h in as many or as few places as you would like, thats the beauty of headers. Kelly said: I am still getting multiple definition errors. I can't see offhand what else is wrong, here are a few comments on stuff though: extern int NewMap(); Its not really important, but extern is assumed for all function definitions. If you wanted to sound smart in front of C geeks you'd want to drop the explicit usage of the word here. You also never implement the NewMap function in dialog.cpp, is there reason for this? Also IIRC your MENU lists should end with a NULL instance, like the dialog does. Aka: MENU main_menu[] = { /* (text) (proc) (child) (flags) (dp) */ { "&File", NULL, file_child, 0, NULL }, { "&Edit", NULL, edit_child, 0, NULL }, { "&About", NULL, about_child, 0, NULL }, { NULL } }; However, I can't see any multiple definition errors in your code. Could you paste your exact compiler command and the resulting errors. Its important that all this information is copied and pasted exactly (in all its jargony glory). |
moon_rabbits
Member #8,469
March 2007
![]() |
NewMap() was never implemented because I was going to write the function, but I got all these multiple definition errors and haven't gotten around to it yet. I removed the declaration from my source code for now just for less clutter while we work this out. So once again, here is all the source + errors: dialog.h:
main.cpp:
dialog.cpp:
And the errors: Quote:
multiple definition of `main_menu' Usually, when I double click an error in Dev-Cpp, it highlights the line where the error occurs. Unfortunately it is not taking me to the first definitions. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote: Unfortunately it is not taking me to the first definitions. Yes this is because it does not know where they are. Did you do a full recompile? Also, the compile command itself would be helpful, you can get it by going to the "Compile Log" tab and selecting everything in that textbox. ** You can also hit control + F11. |
moon_rabbits
Member #8,469
March 2007
![]() |
Thanks for the help~ |
Peter Wang
Member #23
April 2000
|
Quote: Its not really important, but extern is assumed for all function definitions. assumed for function declarations Quote: If you wanted to sound smart in front of C geeks you'd want to drop the explicit usage of the word here.
|
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote: declarations Ah right, I always mix up those two words:P |
|