1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Including Things In C++

  1. Nov 20, 2009 #1
    Hi there everyone.

    I have a program I'm going start writing for class with a bunch of functions I want to keep in separate .cpp files for organizations.

    So what I'm thinking I could do is create a header and have all the command library include commands in there, as well as the include commands for each of the sub-function .cpp files.

    So something like:
    Code (Text):

    #include <library>
    #include <library>
    #include <library>
    #include <library>

    #include 'function.cpp'
    #include 'function.cpp'
     
    Then in the .cpp that houses my main() function I could do this:

    Code (Text):

    #include 'superawesomeheader.h'
     
    Would this work?
     
  2. jcsd
  3. Nov 20, 2009 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Why do that? The compiler (actually the linker) does this for you.
     
  4. Nov 20, 2009 #3
    Really? Don't I have to include the libraries in each .cpp file? Just the main?
     
  5. Nov 20, 2009 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Regarding the #include <function.cpp>:
    Do not do this. This is what I was talking about in my first post.


    Regarding the #include <library>:
    I assume you are talking about something along the lines of #include <iostream>
    You are *not* including the library here. You are including a header file that defines the a set of capabilities. The code that implements those capabilities? Most of that code is not anywhere on your computer.


    Do you understand the difference between a header file and a source file?
     
  6. Nov 20, 2009 #5
    I guess not...our teacher didn't explain it all that well.

    We were just shown one and told to use it in an earlier project.
     
    Last edited: Nov 20, 2009
  7. Nov 20, 2009 #6

    Mark44

    Staff: Mentor

    The source code is probably not on your computer, but the object code (DLLs and LIBs and such) better be.
     
  8. Nov 20, 2009 #7
    I'm confused now. I definitely have the source code, seeing as I wrote it.
     
  9. Nov 20, 2009 #8

    Mark44

    Staff: Mentor

    But you don't have the source code for stuff like printf or cout and a whole lot of other functions that you are using. The source code (human readable) for the standard library functionality is compiled into object code (machine readable) that the linker brings in after the compiler compiles your code.
     
  10. Nov 20, 2009 #9
    Right...I'm lost as to how we got here. I'm just trying to include a bunch of other .cpp files into one and then have a header include some libraries in the whole deal.
     
  11. Nov 20, 2009 #10

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Don't ever #include .cpp files. That's not how the flow is supposed to work.

    - Warren
     
  12. Nov 20, 2009 #11
    Then how does it go together? Do I just paste the completed functions into one file?
     
  13. Nov 20, 2009 #12

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You must compile each .cpp file independently, and then link all the results together into an executable.

    If you're using some kind of visual IDE (Visual Studio, for example), all you need to do is put all of the .cpp files into your project. If you're using a command-line compiler, you need to put the filenames of all your .cpp files onto your compile command.

    Tell us more about how you're building your program, and we can help more.

    - Warren
     
  14. Nov 20, 2009 #13
    Ok then. I'll try that out.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Including Things In C++
  1. #include C++ question (Replies: 2)

  2. C++ . (Replies: 19)

Loading...