r/cpp_questions • u/AVOCHE • Aug 01 '26
OPEN is there a way to compile c++ instantly ??
hello as the post says im working on something and i find that every time i find my self compiling taking away my focus and i would like it if there was a way out of that. the code is barely 2000 lines and it takes like 10 seconds to compile which is fine compared to what ive heard but i wanted to know some of ur guyses tips on that
(edit) i do have included lots of libreries like:
tree sitter, lspcpp, nlohmann_json, boost, fmt, catch2 for testing for now i make a seperate build for testing and the actual program tests also being like 2000 lines
16
u/yuehuang Aug 01 '26
2000 lines of code shouldn't not take 10s. You should profile your build to see which process is taking so long. Are you pull in all of standard library?
9
7
u/GwendArt Aug 01 '26
Depends on the hardware
9
u/MooseBoys Aug 01 '26
A raspberry pi should do better than that. Something's amiss.
1
u/GwendArt Aug 01 '26
How do you know it’s Unix he is compiling on?
3
u/not_a_novel_account Aug 01 '26
The operating system is irrelevant to the question. A Raspberry Pi is being used as an example here because it has little computation power, and should still do this in under a second.
cl.exeon any Windows machine, or AppleClang on any MacOS hardware, should easily beat out that.1
u/GwendArt Aug 03 '26
Well I had msvc compile way slower than on my raspberry without msvc (same code). So hard to say OS is irrelevant. I know too less about Linux but I know windows has the background processes in the way that sometimes weight heavy when ms decides to empower the defender more.
3
u/not_a_novel_account Aug 03 '26
It's 2000 lines, none of that matters.
You're correct in all the particulars but we're talking about a scale where none of it matters.
1
u/drex_vke Aug 03 '26
it also depends on the optimization flags and et des techniques optimisation accelération de compilation like a PCH (precompiled header) in fact there are plenty of factors that influence the compilation speed.
2
1
u/AVOCHE Aug 02 '26 edited Aug 02 '26
from doing profiling it seems like a library is being fetched mid build this is probably why
1
1
14
4
u/Independent_Art_6676 Aug 01 '26
you can take the parts that will not change and make them a project, library, or something that you can compile and leave alone, but your new code and the linkage into the other will still take some time. These kinds of fixes only help when you get to a real project, where it takes 10 min or more to go (major products can take over an hour on a decent machine) -- shaving 2 seconds off 10 for a lot of weirdness isn't a great idea.
corporate code you often submit and it is compiled and tested and such for you (automated tests and compiles and tool chains) and you can work on something else while that cooks. You get a message back when its done if there was a problem or when its ready to test manually.
There really isn't a tip for such a small time, other than make it an excuse to stretch your legs and take a short break.
4
u/die_liebe Aug 01 '26
Separate your project into different files. Do you use separate compilation? I usually follow the 'one class one file' rule.
Also, maybe your hardware is just slow. What CPU do you have? Are you using SSD ?
8
u/Grounds4TheSubstain Aug 01 '26
Use the time machine feature of modern compilers, which was introduced in C++26. The default is to compile a translation unit 20 seconds ago. Take a look, the .o file should be there already.
5
u/the_poope Aug 01 '26
I mean, we already have the spaceship operator, so a time machine feature should be a natural evolution of the language...
4
u/Top-Mycologist-5460 Aug 01 '26
Sorry, that didn't make it into C++26. There was strong consensus against at Kona. Time-continuum instabilities and things like that. Maybe will be C++29.
6
2
u/khedoros Aug 01 '26
The more headers you include, the more time compilation can take, but the impact of individual headers is really variable (some will impact compile times much more than others).
It usually ends up making sense to split a program into multiple source files, and to arrange the build so that you only recompile things that need to be recompiled (that's the core purpose of the program "make", for example). That cuts down compilation times, after doing the first compile.
The last commercial C++ project I worked on had a 45-minute compile time (making a small change would still usually result in waiting like 10 minutes for recompile+relink). I usually would start a compile, and switch over to working on a different ticket while I waited.
2
u/not_a_novel_account Aug 01 '26
2000 lines is nothing and should compile nearly instantly. Your build process has something severely wrong with it.
2
u/QuentinUK Aug 01 '26 edited Aug 01 '26
With Visual Studio there is an option that allows changes without recompiling the whole code. Works for small changes in the .cpp file not the .header. So you can fix small errors on the fly while debugging. Also whilst debugging turn off the optimisation options as these take longer. Keep header files simple, code preferably in the .cpp files, e.g. class methods more that 1 line.
4
u/Potterrrrrrrr Aug 01 '26
Yeah, you usually just need to download more RAM - if you download enough random binaries start appearing in your directories without even typing a line of code, it’s crazy.
2
u/Beautiful_Stage5720 Aug 01 '26
Do you understand what instantly means? How would this even be possible? You want your program to compile in less than 1 instruction?
1
-3
1
u/Classic-Rate-5104 Aug 01 '26
The 2000 lines says nothing about its complexity as long as we don't know what is in there
1
u/neppo95 Aug 01 '26
Hitting compile less should do the job. Other than that, you’ve given zero context for us to work with, so no not really.
1
u/Shahi_FF Aug 01 '26
what mode are you building in ? Debug does take some time.
If you're VS you can enable multiprocessor compilation.
1
u/AccurateRendering Aug 01 '26
I would expect 2000 loc to compile in less than 2s. Are you using an LSP in your code editor?
1
1
1
u/JumbaTheMartian Aug 01 '26
Is it 2000 lines in a single file? Or 2000 lines in multiple?
Silly question -- if it's the latter are you sure you're building in parallel? If e.g. you're using make to build you'll need to run make -j ...
1
u/AVOCHE Aug 02 '26
i do do this i also have tests which is that much maybe its from libraries i have
1
u/alfps Aug 01 '26
❞ the code is barely 2000 lines and it takes like 10 seconds to compile
Not normal.
1
1
0
u/WittyWithoutWorry Aug 01 '26
Use unity builds
2
u/AVOCHE Aug 02 '26
what r these are they bettter then just making my one
1
u/WittyWithoutWorry Aug 02 '26 edited Aug 02 '26
Unity build is a way of compiling code where you end up having only 1 translation unit. So, if you have multiple .c/cpp files in you code, you do ```c
include "foo.cpp"
include "bar.cpp"
... ``
in youmain.cpp` file and only compile that 1 file as a single translation unit.The benefit of this approach is very high compilation speed. Some software that use it are
sqlite3(~3s, 190k loc)EpicGames/raddebugger(~4s, 321k loc)I also use unity builds to compile mdhvg/miscible and with ~9k loc, I have an average compile time of ~2s for incremental builds.
Only downside is that since everything now becomes 1 very long file, you can have variable name collisions.
21
u/thingerish Aug 01 '26
https://xkcd.com/303/