Organizing C++ Functions with Header and Include Commands

  • Comp Sci
  • Thread starter Lancelot59
  • Start date
  • Tags
    C++
In summary, Warren is trying to 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. This would give him a super awesome header file that would include the library capabilities and the main() function.
  • #1
Lancelot59
646
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
  • #2
Why do that? The compiler (actually the linker) does this for you.
 
  • #3
Really? Don't I have to include the libraries in each .cpp file? Just the main?
 
  • #4
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?
 
  • #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:
  • #6
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.
 
  • #7
I'm confused now. I definitely have the source code, seeing as I wrote it.
 
  • #8
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.
 
  • #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.
 
  • #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.
 

Related to Organizing C++ Functions with Header and Include Commands

1. What is the purpose of including things in C++?

Including things in C++ allows programmers to use existing libraries, functions, and classes in their code without having to write them from scratch. This saves time and effort and also promotes code reusability.

2. How do I include a library in my C++ program?

To include a library in your C++ program, you need to use the #include preprocessor directive followed by the name of the library enclosed in angle brackets, like #include <iostream>. This will make the contents of the library available for use in your code.

3. Can I include multiple libraries in my C++ program?

Yes, you can include multiple libraries in your C++ program by using multiple #include directives. It is important to note that the order in which the libraries are included can affect the functionality of your code, so it is recommended to include them in a logical and consistent order.

4. What is the difference between including a library with angle brackets and quotes?

When including a library with angle brackets, the compiler will search for the library in the standard include directories. On the other hand, when including a library with quotes, the compiler will first search for the library in the current directory, and if not found, it will then search in the standard include directories.

5. How do I include my own header files in my C++ program?

To include your own header files in your C++ program, you can use the same #include directive as you would for including libraries, but instead of using angle brackets or quotes, you will enclose the name of your header file in double quotes, like #include "myheader.h". This will make the contents of your header file available for use in your code.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
515
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Programming and Computer Science
Replies
6
Views
982
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Programming and Computer Science
Replies
15
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
921
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
840
Back
Top