Thread: Using #include and header files

  1. #1
    Registered User
    Join Date
    Aug 2007

    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    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
    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, 12:21 PM
  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