Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to include class headers without having to compile their cpp files?

We can include < iostream > and we do not care about its cpp file, but why can't we do the same for our own classes ?

So if my project uses 50 custom classes, not only do I have to have 50 includes, but also have to compile/link 50 cpp files along with them (and clutter the project tree).

Q: Is there any way to use custom headers the same way we use standard libraries ?

In other words is there a kosher way so that we do not have to add all those cpp files in the project. I want to only include ClassSnake.hpp which in turn knows where to find ClassSnake.cpp which links to ClassVector.hpp which knows how to find ClassVector.cpp ... all in an automatic daisy chain without me having to explicitly add those cpp files in my project tree.

Edit: I am not worried so much about the cpp files recompiling. My issue is with having to remember which class internally links to which other class, so that I can properly include all those hidden cpp files in the project tree ... and clutter the tree.

enter image description here

like image 439
Thomas An Avatar asked Nov 29 '25 15:11

Thomas An


1 Answers

Not really.

What you're missing is that your compiler toolchain has already compiled the bits <iostream> needs that aren't in the header.

Your compiler (linker really) just implicitly links this code in without you having to specify it.

If you want to clean up your project tree a bit, you could create other projects that are libraries of code for one main project to use.

like image 104
Collin Avatar answered Dec 02 '25 05:12

Collin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!