• link header files the right way?
    21 replies, posted
Hi, I have a little problem :P I'm so bad at (linking)? header files. I don't know where I should include them. let's say we have 3 headers: model.h texture.h main.h and 3 cpp files: model.cpp texture.cpp main.cpp. the classes in the texture file will be used in the model file. And the texture and model file will be use in the main :P Maby I jsut messed up this but I hope you understand what I mean.
I'd say include texture.h in model.h, texture.cpp in texture.h, model.cpp in model.h and include all the .h files in main.cpp? I'd wait for another reply though. Something might be wrong with your design.
If model.h includes texture.h, then all you need to include to use it is texture.h. And the .cpp files include their header files respectively.
Include the .h file in the .cpp file that needs what's in the .h file. Simple as that.
[QUOTE=gilly_54;23161104]I'd say include texture.h in model.h, texture.cpp in texture.h, model.cpp in model.h and include all the .h files in main.cpp? I'd wait for another reply though. Something might be wrong with your design.[/QUOTE] That's terrible. If you keep doing that for a big project, you will have to recompile the entire project every time you change something, because there's only ever one code module. As Z_guy mentioned, the answer is to include the header files in every module that uses declarations from that header file. Then compile and link all three modules (main.cpp, texture.cpp and model.cpp) with the compiler. Make sure you have header guards too, so that declarations won't appear multiple times in one code module.
I always just mounted #pragma once at the top of my .h files and placed them in every file that would use it, just to be safe. It hasn't increased compile times for me, at least to a noticeable degree.
I always use [cpp]#ifndef _SOMEHEADER_H #define _SOMEHEADER_H /* Code */ #endif[/cpp] Just how I learned. I also heard that it's more universal that #pragma once.
[QUOTE=NorthernGate;23168491]I always just mounted #pragma once at the top of my .h files and placed them in every file that would use it, just to be safe. It hasn't increased compile times for me, at least to a noticeable degree.[/QUOTE] You mean included them? Also, "#pragma once" is not a portable solution. The whole idea of the pragma directive is that it lets you use compiler-specific features. Although chances are his compiler supports it, he hasn't said what compiler he's using, nor if he cares about portability or not. Use include guards unless you know what you're throwing away by using #pragma once.
[QUOTE=jA_cOp;23168615]You mean included them? Also, "#pragma once" is not a portable solution. The whole idea of the pragma directive is that it lets you use compiler-specific features. Although chances are his compiler supports it, he hasn't said what compiler he's using, nor if he cares about portability or not. Use include guards unless you know what you're throwing away by using #pragma once.[/QUOTE] Eh, I guess. I always used it because it was quicker than #ifndef FUCKSHIT #define FUCKSHIT #endif
But it comes down to is Portability > Convenience.
[QUOTE=Agent766;23168829]But it comes down to is Portability > Convenience.[/QUOTE] Depends really. I know the platform and compiler I'm coding with, I know that it works and I'm not working in a team with anyone ATM. Meaning #pragma once is perfectly viable command for me to use.
Or if you use an IDE or text editor with a plugin for it, you can just use a little template adding those automagically.
[QUOTE=Agent766;23168829]But it comes down to is Portability > Convenience.[/QUOTE] Depends entirely on requirements.
1. Use #pragma once while developing. 2. Remove it and tidy up the linking before release. 3. ??? 4. :razz:
[QUOTE=ralle105;23341018]1. Use #pragma once while developing. 2. Remove it and tidy up the linking before release. 3. ??? 4. :razz:[/QUOTE] Release does not mean end of development.
[QUOTE=jA_cOp;23341114]Release does not mean end of development.[/QUOTE] Well, tidy up once you have a solid base set up then, point is #pragma once isn't burned into the code once it's written:colbert:
Why would you even write it as pragma once if you're just going to change it later. It's literally 2 extra lines anyway, I don't see how that's at all hard. [editline]09:57AM[/editline] Actually there's another thing you shouldn't do, include libraries in your public headers unless you really have to. It makes it needlessly painful for users of your library in certain circumstances.
Going to the bottom to add #endif is awkward, so I'd rather do it when tidying up the rest of the file to. As long as pragma once isn't in the final app, does it really matter?
How is it awkward... You write the header include guards before you write anything else. *#ifndef <GUARD>* *enter* *#define <GUARD>* *enter* *enter* *#endif* If that is difficult for you I feel you have some serious issues.
Then serious issues I have. [editline]02:59AM[/editline] I'm really not experienced enough to argue this, I'm just saying my impression on why people would use pragma once.
#pragma once is an optimization to speed up compilation of large projects. Without it, the compiler (well, preprocessor) has to open and read the contents of the file every time it gets #include'd. WIth #ifndef guards, the contents will be ignored, but it still has to read through the whole file anyway -- because there [i]could[/i] be something before the #ifndef, or after the #endif, that needs to be included again. With #pragma once, the compiler can [i]completely ignore[/i] the second and subsequent inclusions of a header. It doesn't even have to open the file, because it recognizes the filename as one that's been included already. In a large complex project with many headers and many #include lines, that can speed up compile times noticeably. Technically, pragmas are always unsafe in portable code because their meaning can be different from one compiler to another: one compiler might interpret the "once" directive as "ignore subsequent inclusions of this file", but another could interpret it as something like "parse this file as FORTRAN instead of C++". In practice, #pragma once is de-facto-standard enough that you're unlikely to encounter any compilers that "support" it in some way other than what you expect. Some compilers may not support it at all, and will just ignore it, so you still need the #ifndef guards. So, from a portability standpoint, it's probably OK to use #pragma once as long as you use it [i]in addition to[/i] guard macros, not [i]instead of[/i] them.
On a performance note: "The GNU C preprocessor is programmed to notice when a header file uses this particular construct and handle it efficiently. If a header file is contained entirely in a `#ifndef' conditional, then it records that fact. If a subsequent `#include' specifies the same file, and the macro in the `#ifndef' is already defined, then the file is entirely skipped, without even reading it." [url]http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC8[/url] #pragma once would be nicer if it were not a #pragma, and instead a standard macro. I think there have been moves for this to happen but not sure. I also note there are a few standard pragmas in C99 if I'm not mistaken. This is how it should be done. [url]http://blog.djmnet.org/2008/08/05/a-pragmatic-decision/[/url]
Sorry, you need to Log In to post a reply to this thread.