Page 1 of 1

Header files

Posted: Fri Jun 21, 2013 10:28 am
by Rebornxeno

Code: Select all

        DataStrcture.h:
struct Data
{
    int a,b,c;
}

Code: Select all

        MakeData.h:
Data MakeData();

Code: Select all

        MakeData.cpp:
#include "MakeData.h"
Data MakeData()
{
    Data d;
    d.a = 1;
    d.b = 2;
    d.c = 3;
    return d;
}

Code: Select all

        Main.cpp:
#include "DataStrcture.h"
#include "MakeData.h"

main(argc, argv[])
{
    Data d = MakeData();
    return 0;
}
Will this work, why or why not?

Re: Header files

Posted: Fri Jun 21, 2013 11:29 am
by Tim Wilson
Why not compile it and find out?

Re: Header files

Posted: Fri Jun 21, 2013 11:56 am
by bbguimaraes
Compiling this on my mind gives me the following errors:

MakeData.h (included in MakeData.cpp): Data is not defined. Include DataStrcture.h (sic) on MakeData.h.
DataStrcture.h (included in Main.cpp): this file is not include-guarded, so you'll have trouble when you include both DataStrcture.h and MakeData.h. I say include-guard everything and don't care about it.

Then

Code: Select all

gcc -c MakeData.cpp
gcc -o a_program_that_should_work Main.cpp MakeData.o
should work. Always take care to declare everything before it is used and never declare the same name twice. Header files are no secret: you just have to keep in mind how they work, which is literally replacing the "#include <whatever>" with the contents of the file "whatever".

Re: Header files

Posted: Fri Jun 21, 2013 1:50 pm
by Rebornxeno
What if instead of a struct, I had a #define. If I have to include that in every file, I'll get macro redefinitions. What then?

Re: Header files

Posted: Fri Jun 21, 2013 1:58 pm
by bbguimaraes
Rebornxeno wrote:What if instead of a struct, I had a #define. If I have to include that in every file, I'll get macro redefinitions. What then?
// MyMacro.h
#ifndef __MY_MACRO_H__
#define __MY_MACRO_H__

#define mymacro whatever

#endif

Re: Header files

Posted: Fri Jun 21, 2013 2:44 pm
by Rebornxeno
Are you sure that works? I have a file with #pragma once and #ifdef guards and I'm still getting macro redefinitions.

Re: Header files

Posted: Fri Jun 21, 2013 3:03 pm
by bbguimaraes
Wait, what are you trying to do? Macro redefinition errors (should) only occur when you #define a macro twice *in code*. Including the same file should not change the macro.
// mymacro.h
#define mymacro() 42
// some_other_file.h
#include <mymacro.h>
int f() { return mymacro(); }
// test.c
#include <mymacro.h>
#include <some_other_file.h>
int main() { return f(); }
// sorry, c code standards
That is "ok". You should put include guards on mymacro.h to avoid redefining the macro when you include both mymacro.h and some_other_file.h on test.c, but this is technically not an error. An error would be adding "#define mymacro() !42" somewhere (note: "#define mymacro() 42" is fine, check the link above). That issues the warning:
/tmp/some_other_file.h:2:0: warning: "mymacro" redefined [enabled by default]
/tmp/mymacro.h:1:0: note: this is the location of the previous definition

Re: Header files

Posted: Fri Jun 21, 2013 3:19 pm
by Rebornxeno
I'm getting macro redefinition warnings, like 300 of them. Huge pain in my ass every time I recompile. I could probably fix it if my ide told me where the first and second definitions were, but it only tells me "warning C4005: 'DXGI_STATUS_OCCLUDED' : macro redefinition". Double clicking the warning takes me to the location in the file where this definition appears, and searching through the file tells me that it is only defined here once. It is header guarded, and every single header file I use is header guarded with #pragma once and #ifndef guards. Every time I include this header in another place, I get like 75 more redefinition warnings. My ide is vs2012. So apparently header guards aren't stopping it, or the gods are frowning at my lack of belief in them.

My first warning is "warning C4005: 'DXGI_STATUS_OCCLUDED' : macro redefinition"
my 33'rd warning is "warning C4005: 'DXGI_STATUS_OCCLUDED' : macro redefinition"
and one for every time it's included, all from the size file, on the same line.

Re: Header files

Posted: Fri Jun 21, 2013 3:32 pm
by bbguimaraes
Oh, OK, so the macro isn't yours? That may be a problem with the library you're using. A quick google search says it's a conflict between winerror and d3dx:

http://stackoverflow.com/questions/1266 ... winerror-h
http://xboxforums.create.msdn.com/forums/t/108223.aspx

Re: Header files

Posted: Fri Jun 21, 2013 3:42 pm
by Rebornxeno
Yep! I just got through reading that myself, fixed it, and the warnings are gone. I can't tell you how annoying 300 warnings are. Thanks for the help!

Re: Header files

Posted: Fri Jun 21, 2013 3:47 pm
by bbguimaraes
Yeah, that's got to be annoying. Strange that it is a "feature" of legacy DX development on windows 8. But good thing you got rid of it.