Organizing C++ Functions with Header and Include Commands

  • Context: Comp Sci 
  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around organizing C++ functions using header files and include commands. Participants explore the proper way to structure a C++ program with multiple .cpp files and the implications of including libraries and source files.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant suggests creating a header file to include all necessary libraries and sub-function .cpp files for organization.
  • Another participant questions the need for including libraries in each .cpp file, implying that the linker handles this.
  • A participant emphasizes the difference between header files and source files, stating that including .cpp files directly is not advisable.
  • There is a discussion about the availability of source code for standard library functions, with some participants asserting that the source code is not available on the user's computer.
  • One participant expresses confusion about how to properly include functions from multiple .cpp files into a single program.
  • A later reply clarifies that each .cpp file should be compiled independently and then linked together to create an executable.

Areas of Agreement / Disagreement

Participants express differing views on the proper method for including files in C++. There is no consensus on the best approach to organizing functions across multiple .cpp files, and confusion remains regarding the inclusion of libraries and source files.

Contextual Notes

Some participants demonstrate uncertainty about the role of header files versus source files, and there are unresolved questions about the compilation and linking process in C++ programming.

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 count 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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
4
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K