So, I am working with c++. I know how to link .h file with .cpp (pretty simple stuff.) Problem I am having is that, I don't want to write all my code in one .cpp file, that makes it too big and organization becomes a hustle. In other languages (c# and python) I was able to write a class in a different file then derive children from it, much like header file in c++, but .h files are used only for declaration of the functions and .cpp is where everything is being coded. So, without having a one large .cpp file, can I code it in multiple .cpp files?
Asked
Active
Viewed 191 times
-4
-
8Almost all C++ projects consist of multiple .cpp and multiple .h files. – Jul 25 '17 at 19:29
-
2Be careful with language. "link" has a very special meaning in the context of C and C++. I don't think you understand linking yet (or you wouldn't be asking this question). Say instead that you know how to include a .h file into a .cpp file. – Gillespie Jul 25 '17 at 19:36
-
http://www.cprogramming.com/compilingandlinking.html – Gillespie Jul 25 '17 at 19:37
-
"Is it possible to have more than 1 .cpp file in a project" - Yes. Of course it is. Have you ever looked at a real project? Try digging into any popular open-source project and you'll see *many* source files in the same project/directory - how would we ever organize code if it all had to go into one monster file? – Jesper Juhl Jul 25 '17 at 19:39
-
1I have projects with several thousand source files. I am sure there are a few hundred in a single folder some where.. I usually subdivide into subprojects. – drescherjm Jul 25 '17 at 19:39
-
1May I suggest reading [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (or two).. – Jesper Juhl Jul 25 '17 at 19:47
-
Thanks guys. I am reading a book @JesperJuhl, its called The C++ Programming Language by Bjarne Stroustrup, just wanted quick insight. What I didn't know was that I didn't have to setup physical connection (using #include or something), all I needed to do was to have both .cpp file call the same .h file. – Mohammad Rahman Jul 25 '17 at 22:03
1 Answers
2
Sure. You have one header file with the relevant declarations and then you can have multiple source files implementing them. You only need to make sure that they all are linked together.
It is possible, because when you link them together, it doesn't matter from which translation unit the definitions come from, the only thing that matters is that they exist. There would be no difference if you would have implemented them all in the same translation unit.
It would look like this:
// header.h
// guards...
void func1();
void func2();
// source1.cpp
#include "header.h"
void func1() {}
// source2.cpp
#include "header.h"
void func2() {}

Rakete1111
- 47,013
- 16
- 123
- 162
-
-
So, their linker is the .h file, I don't have to #include or #using or anything? for example, I will code in source2.cpp and complete the function, then on source1 (main source) I would just call the .h file and this will inherit the function I wrote in source2? – Mohammad Rahman Jul 25 '17 at 19:39
-
1@MohammadRahman There is no such thing as "function inheritance". If you include `header.h`, you can use the functions declared in them providing they have a definition, that all gets resolved by the linker *after* compilation. You also shouldn't link header files, only source files – Rakete1111 Jul 25 '17 at 19:41
-
2@MohammadRahman There's nothing wrong with the questions you're asking, but they suggest you need a much better C++ reference material than what you're working with. Different compiler suites have different approaches to organizing dependencies, where Visual Studio, Xcode, and `gcc`/`clang` with `make` or `cmake` all have various ways of coordinating this. While the fundamentals are similar, you should dig deeper on how your tools work. – tadman Jul 25 '17 at 19:46
-
@Rakete1111 let me clarify, So, with your set up, if I define what func2 does in source2, then if I call func2 in source1, it will do what I defined in func2 in source2. Is this correct? – Mohammad Rahman Jul 25 '17 at 20:17
-