I'm a Java developer and I never have to worry about including files or messing with BUILD files.
Whenever I need to write C++ code, things get more complicated. I can think of creating *.h files as interfaces in Java, but figuring out how to write the build file and what order classes should be included in gives me a headache.
Is there a simple way to think of this? How do you know when to include something or how to separate things out properly. For example, what is generally a good way to deal with a project with dozens of sources files that are interdependent on each other.
Is there some framework to make creating BUILD files or managing all this boilerplate compilation stuff more bearable?
CMake is the best build system I've been able to find so far. You give it a list of your source files, and it will automatically scan dependencies and recompile only changed files. Although its syntax is a bit funny, and documentation is not very accessible, CMake beats GNU autotools in usability and simplicity, and it works on all major platforms.
As to your "mental model" of what's going on, here are some points to keep in mind.
A .cpp
file is compiled completely independently of other .cpp
files.
The .cpp
file is read by the compiler from top to bottom, only once. Hence, things need to be in the proper order.
A #include
directive is the same as copy/pasting the header into the .cpp
file.
At the point where a function is used, a declaration of that function is needed, but not necessarily a definition.
At the point where a class member is accessed, a definition of the class is needed. Deriving from a class also requires its definition. Taking pointers or references does not require a definition, but does require a declaration. Use this to your advantage in headers: instead of including Foo.hpp
, see if you can get away with just a declaration of class Foo;
.
When compiling a .cpp
file, a .o
file is generated that contains the implementation of exactly those functions defined in the .cpp
. References to functions not defined therein are left for the linker to resolve.
The linker puts all these definitions together into an executable, but each function definition has to be present exactly once. (Templates and inline functions are an exception.)
I am a big fan of stackoverflow podcast and I have decided that when I would use a build system I should use FinalBuilder.
Jeff Atwood and Joel Spolsky had a conversation about that and it is mentioned that it is used in Fog Creek.
The podcast is here
FinalBuilder is here
Feature Tour
I hope it is suitable for the purpose.
It works on the Windows platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With