Setting up headers, functions etc.

This is a discussion on Setting up headers, functions etc. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm fairly new to c++. I'm scratching my head a bit about how I need to have my project ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    158

    Setting up headers, functions etc.

    Hi, I'm fairly new to c++. I'm scratching my head a bit about how I need to have my project setup to work the best way. Like where to put includes, function prototypes and definitions in order for my whole program to be able to use them.

    Right now my program consists of several .cpp files with a lot of functions and one header file with a few inline functions. Alll of the .cpp files are using #include on the header file and the header file itself has many includes. This makes it not very efficient since that means a lot of code is duplicated in each .cpp file.

    Any tips about how to set things up to work efficiently?

    Thanks,
    Carl-Mikael

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Each cpp file, except possibly the main functions c++ file, should be associated with a single header file of all the functions in it, or of the object declaration to which the cpp file provides method declarations.

    Header files should include all the other headers that are required to use call the function it prototypes or defines and to instantiate any objects that are contained in any object seen in the header.

    Source (cpp) files should include their associated header, as well as any additional headers that are necessary to implement the functions and methods the object defines.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Btw, I'm using Visual Studio 2005...

    One problem I'm having is that if I write the function prototypes in the header file the compliler will complain that those functions are already defined since each .cpp file includes the header file...:/

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Hi King Mir, thanks for the reply.

    If I have functions in different .cpp files that all need to include a certain header file, is it not possible to only include that header once within the project or do each .cpp file need to include that same code?

    Also the is no main() function in my program. I'm doing a plugin for a 3d program that exposes new c++ functions to the 3d programs scripting language.
    Last edited by DrSnuggles; 10-22-2007 at 08:16 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    One problem I'm having is that if I write the function prototypes in the header file the compliler will complain that those functions are already defined since each .cpp file includes the header file...
    Use header guards, e.g.,
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    /* insert function prototypes, class declarations, etc here */
    
    #endif
    Of course, HEADER_H has to be unique to the header file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The repeated include of headers is a necessity in C and C++. It can be inefficient, especially in the case of some C++ libraries, but there is no alternative.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DrSnuggles View Post
    Also the is no main() function in my program. I'm doing a plugin for a 3d program that exposes new c++ functions to the 3d programs scripting language.
    But you'll presumable have one place where your plugin is "started" from - or some other form of "entrypoint". The fact that it may not be called main doesn't REALLY change things that much.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Including headers within headers can sometimes increase compilation time. I usually avoid including other headers within headers, if possible (it also avoids circular includes such as header A includes header B and header B includes header A). Might also be a good idea to only include headers in your project, and not any C++/platform sdk headers. It can also be a good idea to include C++/platform sdk headers in a precompiled header stdafx.h since it includes huge amount of files/data.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Including headers within headers can sometimes increase compilation time. I usually avoid including other headers within headers, if possible (it also avoids circular includes such as header A includes header B and header B includes header A).
    But not having each header file "complete" is a problem - it relies on the user knowing that header A is needed for header B to work correctly. This gets VERY annoying if you have a large project where lots of files are dependant on other files - you know you need header A, but you don't know that header B has to be included first - you just get a whole bunch of errors because of undeclared stuff - and it can take quite some time if this is complex and B also relies on C, C relies on D, D relies on E & F.

    There is one exception, and that is if you have a single headerfile that is needed by just about every component anyways [e.g. the "base of the whole system"].

    As to A including B that includes A: You don't get that problem as long as your include of B inside A is inside the include guard - which is how it should be. If you don't use include guards, then you really are in for trouble with getting all the includes sorted out.

    Might also be a good idea to only include headers in your project, and not any C++/platform sdk headers. It can also be a good idea to include C++/platform sdk headers in a precompiled header stdafx.h since it includes huge amount of files/data.
    Using "one include that contains everything" isn't a particularly good idea tho', unless you have a situation where you have something that you HAVE to include everywhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Hey, thanks for the replies!

    laserlight - Thanks that could be something I need, although I think I do need a better overall desgin of the program too.

    CornedBee - That's to bad, I would think it would be more logical to have an header that could be global for all .cpp files in a project. I currently have 10 cpp files which pretty much all need to include the same header files, that will mean that my program get 10 times as large from includeing the same headers over and over correct? Or can I perhaps assume that a well written external header will not define values twice?

    matsp - Yeah I guess you are right. There is an initialization function that does nothing right now.

    Elysia - It seems like it can turn into quite a puzzle to determine which files include what.

    matsp - Thanks for the info

    cheers,
    Carl-Mikael
    Last edited by DrSnuggles; 10-24-2007 at 03:00 AM.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    While I'm at it I'd like to know if you know what this warning means...is it a setting I need to change in the project?

    LINK : warning LNK4013: image size 0x22000 exceeds specified maximum 0x20000

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DrSnuggles View Post
    Hey, thanks for the replies!

    laserlight - Thanks that could be something I need, although I think I do need a better overall desgin of the program too.

    CornedBee - That's to bad, I would think it would be more logical to have an header that could be global for all .cpp files in a project. I currently have 10 cpp files which pretty much all need to include the same header files, that will mean that my program get 10 times as large from includeing the same headers over and over correct? Or can I perhaps assume that a well written external header will not define values twice?
    A well-written header file should use "include guards", which is something like:
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    ...
    ... All the stuff inside the header goes here. 
    #endif // HEADER_H
    Since the preprocessor evaluates the #if bits, it means that the first time HEADER_H isn't set, so:
    1. HEADER_H is now being "set" by the #define
    2. All the stuff of that header is being declared, defined, etc.
    Second time the header is included, it turns into "nothing". So if you include the same file 10 times over, it's still only "used" once. Yes, the compiler (preprocessign stage) will open the file, look at the contents to see that it's not needed, but this is fairly low overhead as long as you don't have literally hundreds of include files. If you really have LOTS of header files, there is an argument to put them into a "everything_you_ever_need.h" - or perhaps your overall software architecture is a bit off if you need ALL headers in ALL .c/.cpp files.

    matsp - Yeah I guess you are right. There is an initialization function that does nothing right now.

    Elysia - It seems like it can turn into quite a puzzle to determine which files include what.

    matsp - Thanks for the info

    cheers,
    Carl-Mikael
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by DrSnuggles View Post
    CornedBee - That's to bad, I would think it would be more logical to have an header that could be global for all .cpp files in a project. I currently have 10 cpp files which pretty much all need to include the same header files, that will mean that my program get 10 times as large from includeing the same headers over and over correct? Or can I perhaps assume that a well written external header will not define values twice?
    You can assume that a well-written header doesn't define anything twice that the compiler cannot merge on linking. Having large headers increases your compilation time, not the size of the resulting executable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Quote Originally Posted by matsp View Post
    But not having each header file "complete" is a problem - it relies on the user knowing that header A is needed for header B to work correctly. This gets VERY annoying if you have a large project where lots of files are dependant on other files - you know you need header A, but you don't know that header B has to be included first - you just get a whole bunch of errors because of undeclared stuff - and it can take quite some time if this is complex and B also relies on C, C relies on D, D relies on E & F.
    It can It's quite a complex situation. One way is to declare that header A, B and C is required to be included before this header and they're included before the header in the .cpp file. But that doesn't always work either.

    As to A including B that includes A: You don't get that problem as long as your include of B inside A is inside the include guard - which is how it should be. If you don't use include guards, then you really are in for trouble with getting all the includes sorted out.
    Instead you'll get an undeclared error and you'll start tearing you hair out because you know you've included the right header before

    Using "one include that contains everything" isn't a particularly good idea tho', unless you have a situation where you have something that you HAVE to include everywhere.

    --
    Mats
    I find it terrific to include headers that do not change. If they don't change they can all be precompiled and you never have to wait for them to be compiled again. After all, declarations are just that - it's no big worry if they're included everywhere since they don't contribute to any more code.
    Of course, there are guidelines and you may have to be careful about things such as #define, but otherwise I always think it's a good solution.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Instead you'll get an undeclared error and you'll start tearing you hair out because you know you've included the right header before
    Huh? If A relies on B, so A includes B, B relies on A and includes A, then by definition, since A has include guards, including it AGAIN inside B is safe and correct. Since both rely on each other, you can't include one before the other anyways, right? [This also means that you have to put the #include <B.h> somewhere below the top of A, which is UGLY and NASTY, but sometimes necessary]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 06:05 PM
  3. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Variables do not equal functions!!!
    By me@burk. in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 05:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21