3

Let say we have a large code base and we are doing development in C++. Do we have to recompile everytime in order to test the code?

If yes then it is going to take ages to do development.

What's the solution to this problem?

petezurich
  • 9,280
  • 9
  • 43
  • 57
Khanakia
  • 723
  • 1
  • 8
  • 20
  • 2
    The answer is yes. Tools like [ccache](https://ccache.samba.org) help. Also make sure you use a build system with proper dependency tracking so it only rebuilds what needs to be rebuilt. – Jesper Juhl Mar 03 '18 at 08:58
  • Usually theres a difference between build and rebuild. Where build would only recompile the affected obj files. Idk how gcc and clang handles this though. – Neijwiert Mar 03 '18 at 08:59
  • Just recompile files that have changed. Divide your project into libraries. Notice that the exact same problem happen to all programming languages. The fact it is interpreted or compiled does not change this fact. – Oliv Mar 03 '18 at 09:00
  • Usually you organize your code in modules e.g. libraries, then you do not need to rebuild all, only the module you changed. – AndersK Mar 03 '18 at 09:01
  • 2
    "What's the solution to this problem?" It's called a *build system*, e.g. [Make](https://www.gnu.org/software/make/), or [CMake](https://cmake.org/), or [Scons](http://scons.org/) – Mike Kinghan Mar 03 '18 at 09:06
  • You should support something called "incremental build". For example when using GCC and `make`, you may use GCC's flag `-MMD` to generate makefile rules to completely support building only the changed part of code. – scrutari Mar 03 '18 at 09:20
  • You can adopt single compilation unit source code organization. It will drastically reduce compilation time so full rebuild won't be a problem. – user7860670 Mar 03 '18 at 09:32

1 Answers1

6

Yes, you'll definitely need to compile C++ code if you want to test it. C++ code can't be executed without being compiled.

However, if you organize your project smartly, compilation could take only a few seconds, or maybe up to a minute, even if there are thousand (or even more) of files.

By default, your build system will run an incremental build, except if you explicitly request a "rebuild" or did a "clean" previously. It will then invoke the compiler/linker accordingly and make sure it compiles/links only what needs to be (if a cpp file did not change, no need to compile it, this is all based on file timestamps, if the "object" file (generated) is older than the cpp file (source), the build system knows it's up-to-date and won't generate it again. If you use Visual Studio and/or CMake or whatever IDE, build system, they all support that!

Additionally, you can follow some guidelines to make this even faster:

Firstly, organize your project in modules (libraries), ideally with dynamic link. Then when a file from a library is changed, only this library needs to be compiled (other libraries or programs using the modified library won't have to be compiled again).

When you'll modify only an implementation file (cpp file), only this file + link of the module using it will be needed.

When you'll modify an header file (h file), all cpp files including it will need to be recompiled, so you must be careful to optimize your includes. Prefer forward declaration (see why here)to includes whenever it's possible (else, your header becomes a dependency of all cpp files using the other header file including yours...as a cascade, modifying this header file will end up requiring compilation of tones of cpp files). Don't include files you don't need (because it will fire a new useless build when the header file changes). Possibly use precompiled headers to speedup compilation.

Note: As commented, there are apparently some tools that can interpret C++ without compiling it...but that's not what C++ was designed for at the first time. And I doubt they will be fast as compiled code at runtime....so you'll probably save 20sec of incremental build time and then loose minutes at runtime....

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • 2
    > "C++ code can't be executed without being compiled." Well, strictly speaking it is possible to interpret c++ programs, and there are tools for that: https://stackoverflow.com/questions/69539/have-you-used-any-of-the-c-interpreters-not-compilers . Not that they have gained much popularity in C++ development. – Grigory Rechistov Mar 03 '18 at 09:16
  • @GrigoryRechistov: Thanks, did not know, I'll update my post. – jpo38 Mar 03 '18 at 09:18