Header files

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Header files

Post 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?
Tim Wilson
ES Beta Backer
ES Beta Backer
Posts: 9
Joined: Tue Jan 22, 2013 2:20 pm
Programming Language of Choice: C++ & Python
Location: Western Pennsylvania

Re: Header files

Post by Tim Wilson »

Why not compile it and find out?
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Header files

Post 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".
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Header files

Post 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?
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Header files

Post 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
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Header files

Post by Rebornxeno »

Are you sure that works? I have a file with #pragma once and #ifdef guards and I'm still getting macro redefinitions.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Header files

Post 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
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Header files

Post 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.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Header files

Post 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
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Header files

Post 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!
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Header files

Post 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.
Post Reply