• Advertisement
Sign in to follow this  

Header Files

This topic is 1659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Very beginner question but how the heck do header files work?

 

I'm using RakNet and have a cpp file with a bunch of functions using it and its variables.  Works great.  Then I made a function which was declared (?) in a .h file, as per below:

 

int getplayernumberfromguid(RakNetGUID id);

 

I then get a TON of "error C2065: 'RakNetGUID' : undeclared identifier" errors for the same line.

 

So, I figure, in my infinite wisdom: no problem - I'll just copy and paste the #include stuff from the top of the cpp file into the top of the h file.  If it works for one, it should work for the other.  Well, that just brings in a mess of new errors including errors inside the RakNet files which should have no issues whatsoever.

 

Any idea what I'm doing wrong?  How are you really supposed to do this?

 

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement

Can you post every source/header file relevant to the problem (provided it's not too long)? It's hard to follow exactly what you did and what you could have done wrong.

Share this post


Link to post
Share on other sites

Thank you very much Trienco.  That actually clearly taught me quite a bit!  Here are the two relevant files.

 

networking.cpp

http://pastebin.com/PGpfVf9d

 

networking.h

http://pastebin.com/1Q77HBdZ

 

The latest change I made was trying to put

int getplayernumberfromguid(RakNetGUID id);

at the bottom of the h file to declare it, if that's the right word.  That's when this mess started.  If I declare it at the top of the cpp file then it all works.

 

By the way, I did also noticed that RakNetGUID is declared in RakNetTypes.h and I tried adding that too but it makes no difference.  Plus it was never needed for my cpp file so maybe it's declared or linked to in another h file??  Don't know.

 

My other guess is that it may have something to do with:

using namespace RakNet;

Not sure how that works across various files but including it in the h file didn't seem to help.

 

I may be over my head with this.  I'm still a little confused as to how what I'm doing could possible start generating errors in RakNet's code (which I didn't touch).

 

(As as sidenote, it's a little intimidating to know that at any time my code can break because I don't know enough and at one point maybe it'll be too complex for anyone to help.)

Share this post


Link to post
Share on other sites

I haven't read the article yet but I moved all but network.h includes to the header.  Now I get a bunch of redefinition errors, like so:

 

1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h(91): warning C4005: 'AF_IPX' : macro redefinition
1>          C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winsock.h(460) : see previous definition of 'AF_IPX'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h(131): warning C4005: 'AF_MAX' : macro redefinition
1>          C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winsock.h(479) : see previous definition of 'AF_MAX'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h(168): warning C4005: 'SO_DONTLINGER' : macro redefinition
1>          C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h(212): error C2011: 'sockaddr' : 'struct' type redefinition
1>          C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winsock.h(485) : see declaration of 'sockaddr'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h(390): error C2059: syntax error : 'constant'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\ws2def.h(390): error C3805: 'constant': unexpected token, expected either '}' or a ','

etc.

 

I'll have to reread these posts to get a better understanding and probably that article.

(I assume, by the way, that having two cpp files include network.h isn't an issue...)

Edited by GameCreator

Share this post


Link to post
Share on other sites

Only include winsock2.h

 

no ws2def.h

 

Its some time back that I created windows applications. But I think you can even ignore windows.h because it is included somewhere deep inside the winsock2.h

 

Anyways winsock2.h must be included before windows.h

Share this post


Link to post
Share on other sites

I don't have those includes in my code but RakNet does (and it's smart enough to know which goes first).  However, I'm using a second library which uses windows.h.  So... by moving the headers, is there a chance that their order got switched?  Because it was working before and all I did was move the headers from my network.cpp to my network.h (which is included in at least 2 cpp files).

Edited by GameCreator

Share this post


Link to post
Share on other sites

Think about two different scenarios.

 

A library/subsystem creator who has defined a lot of structures that need a specific order is the responsible that wants to make the usage, and thus the inclusion of headers, as easy as possible. This is why you find system header include statements in other include files. This makes the usage for the library function as easy as possible.

 

System-Header files are enclosed in <>.

 

The developer of a system/subsystem does not include his own header files from within header files, to have a better control about what is included within a single source files. Circular includes or other problems with undefined or double defined structures/defines and the like can be overcome if the application includes are only put into the source.

 

Application specific header files are enclosed in "".

 

Using <> or "" has todo with the search order of the pre-processor for include files.

 

Another thing is that pre-processor and compiler are two different things. The pre-processor creates a stream of text that results of processing any #directive. The compiler processes that stream after that processing has done. You can tell the compiler to only make the pre-processing step an put the result into a file/stdout. This way you can investigate the the order of files that are included and what the result of the pre-processing is.

 

Think of a define you made different to some system header file. You will not find the bug without have a look on the input stream to the compiler.

 

Share this post


Link to post
Share on other sites

Think about two different scenarios.

 

A library/subsystem creator who has defined a lot of structures that need a specific order is the responsible that wants to make the usage, and thus the inclusion of headers, as easy as possible. This is why you find system header include statements in other include files. This makes the usage for the library function as easy as possible.

 

System-Header files are enclosed in <>.

 

The developer of a system/subsystem does not include his own header files from within header files, to have a better control about what is included within a single source files. Circular includes or other problems with undefined or double defined structures/defines and the like can be overcome if the application includes are only put into the source.

 

Application specific header files are enclosed in "".

 

Using <> or "" has todo with the search order of the pre-processor for include files.

 

Another thing is that pre-processor and compiler are two different things. The pre-processor creates a stream of text that results of processing any #directive. The compiler processes that stream after that processing has done. You can tell the compiler to only make the pre-processing step an put the result into a file/stdout. This way you can investigate the the order of files that are included and what the result of the pre-processing is.

 

Think of a define you made different to some system header file. You will not find the bug without have a look on the input stream to the compiler.

 

This is very interesting i really didn't know what was behind the compiling process, can you recommend a couple of books about this kind of topics?

Share this post


Link to post
Share on other sites

Öh.

I learned this about 30 years ago. It is part of any C/C++ compiler description. On unix-style machines and I think on solaris as well the C pre-processor is a separat program cpp named and you will find descriptions on the man-pages.

 

I never used books to learn things. So I cannot recommend one.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement