Using #include and header files

This is a discussion on Using #include and header files within the C++ Programming forums, part of the General Programming Boards category; Well, I've checked up references and the tutorials. There seems no trace of this, but pardon me if i'm wrong. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    Using #include and header files

    Well, I've checked up references and the tutorials. There seems no trace of this, but pardon me if i'm wrong.

    I'm working on my little stepper motor control project, and it has became large enough that I need to split it in several files.

    Basically, I want something like include or require in php. That simply reads a file and spits it where the function is.
    I went with #include, and pasted a bunch of my functions in another file. Then, at the place where i cut them, i wrote #include "functions.cpp"

    However, the code doesn't work anymore and i get a undeclared for each function.

    How exactly are header files and include used?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Use them the way you would use include_once or require_once in PHP: to include a set of functions or classes that you have written for use in the current source file.

    Basically, if no templates are involved, place the function prototypes and/or class declarations in the header file. Place their implementation in some source file. Include the header (not the source file) in your other files. To get the include_once effect, use inclusion guards for the header files.

    If templates are involved, then just place the entire function/class in 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

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    85
    This is a really odd mechanism, you know. I got it working.. but for example

    If function func() isolated in other.h requires a string, I'll include <string> in other.h, so that its implementation can have access to the string type. Now, if my main program also requires a string, I'll include <string> again. The compiler does its job on other.h, then main. Next step, linking the two.

    Doesn't this end up taking more space?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    not significantly with header guards.

    linking doesn't work on source files, just object files and things like that.

    edit: I misunderstood your question.

    if string is required in the implementation, then include it in the source file (cpp etc), but not in the header file unless it is part of the function prototype or something.
    Last edited by robwhit; 08-12-2007 at 09:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  2. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  3. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

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