Comp Sci Organizing C++ Functions with Header and Include Commands

  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
Organizing C++ functions requires using header files and separate .cpp files for better code management. It's important not to include .cpp files directly; instead, each .cpp file should be compiled independently and linked together. Header files should contain declarations and include necessary libraries, while the main program file includes the header. The standard library functions are accessed through header files, which define capabilities, while the actual implementation is compiled into object code. Properly structuring your project in an IDE or through command-line compilation will streamline the process.
Lancelot59
Messages
640
Reaction score
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:
#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:
#include 'superawesomeheader.h'

Would this work?
 
Physics news on Phys.org
Why do that? The compiler (actually the linker) does this for you.
 
Really? Don't I have to include the libraries in each .cpp file? Just the main?
 
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?
 
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:
D H said:
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.
The source code is probably not on your computer, but the object code (DLLs and LIBs and such) better be.
 
I'm confused now. I definitely have the source code, seeing as I wrote it.
 
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.
 
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.
 
  • #10
Don't ever #include .cpp files. That's not how the flow is supposed to work.

- Warren
 
  • #11
Then how does it go together? Do I just paste the completed functions into one file?
 
  • #12
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
 
  • #13
Ok then. I'll try that out.
 
Back
Top