I am using sublime text 3 with pre compiled headers as mentioned in this blog.
The build that I am using is as follows :
{
"cmd" : ["g++ -std=c++17 $file_name -o $file_base_name && timeout 4s ./$file_base_name <input.txt >stdout.txt 2>stderr.txt"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}
If I use <bits/stdc++.h>, it takes 1.2s to compile and run every time I run any code( eg. Hello World code) using Ctrl+B.
When I use "bits/stdc++.h" (and expect the compilation time to become less) it actually takes 5-6 s to compile and run even the "Hello World" program for the first time using Ctrl+B. From next time, it takes 0.3 s only but the first time, it takes as long as 5-6s.
For every new program that I write, it happens like this. For eg. If I change the "Hello World" program to print 10 numbers from 1-10, it will again take 5-6s to compile and run. Then from the second time, it will take 0.3-0.4 s.
The directory structure is as follows :
CP/
- bits/
- stdc++.h
- stdc++.h.gch
- code.cpp (this is the file in which I write the code)
I have tried finding some information online but couldn't find anything useful. Can someone please help me out?
Idk why this is downvoted but it is quite interesting to observe this only on the first run. I have observed it too.
I guess in the first run compiler has to verify that the pre compiled header is indeed valid to use. Every subsequent runs benefit from one time check?
Same with me the first compilation takes too much time and the subsequent ones compiles instantly. Weird indeed :(
What should be the fix to this ?
I haven't found any fix as of yet :(
Try precompiling the original stdc++.h header from the g++ folder (if later you want to undo this step, simply delete stdc++.h.gch from the same folder), then change "bits/stdc++.h" to <bits/stdc++.h>
Did you precompile the headers with the same flags as you are trying to compile your program?
I have the same issue, in my case it take around 50s. I have tried numerous attempts but it's the same every time. I use the same flags to compile. I even precompiled iostream in the same way but its of no use. I thought that it was just me who had this issue cuz of my old laptop.
Anyways I usually code in Python and it works well in my PC. But this issue with C++ needs to be solved and I will update if I find any solution.
Editv I use VSCode so it's not text editor specific.
I also followed the blog exactly but the issue is still there. I wonder why does it happen only with a few ?
Try adding the
-H
compiler flag to check if the compiler is using the precompiled headers during compilations. i.e :If you are using a precompiled "bits/stdc++.h", it would show a successful hit depicted by an ! :
Else it would show a miss depicted by X and show all the files it's compiling :
Thanks this helped!! Now it's fixed.
After adding "-H", I noticed that even though it took a lot more time to compile in the first time but still the precompiled header was used. So there must be some problem in finding the path to that file.. So What I did was:
I added "-H" in default list of flags which I used in compiling.
Updated VSCode's default compiler path to where my g++.exe was located. In my case "C:\MinGW\bin".
Tested it on a few files and it's workinv fine.
luctivid, it is working for you now but I think it will become like before after some time. It has happened with me too. Once I freshly compile stdc++.h, it works fast in the beginning and then goes back to its previous state after some time.
Well, it has not happened since then. Also you can try these additional steps if it is still not resolved in your case.
Check for an extra
mingw
folder in your working directory or root/C:. In case, you can temporarily delete it or move it somewhere else.use
-v
and-ftime-report
flags during compilation and check for the possible issue.How hard is it to just include some proper standard library headers? It only takes a few seconds to type it. I don't get why people are so dependent on
#include <bits/stdc++.h>
.I had the same problem in windows. Although there was no problem in compilation time but the running time of freshly executable was around 7s — 10s and later executions in just 0.3s. I solved this issue by turning OFF my ANTI-VIRUS. Anti-virus check for viruses in newly created files (in our case *.exe's), which takes around 5s — 6s. After a short while, I switched to Ubuntu 20.04 and had never faced it again.
If you have a different directory for header files (or even precompiled header's), hope you are -I flag in your compilation command.
Thanks for the reply.
What does -l flag do?
I am using Ubuntu 16.04 hence the problem can't be because of anti virus.
If you add -I flag, it will tell compiler too looks for header files in this directory first, and then in its include directory(which is usually "/usr/include/x86-64-linux-gnu/c++/9/bits"). Let's assume you have a header file in the directory /folder1/folder2/folder/3/folder4/bits/stdc++.h. Then you should add "-I /folder1/folder2/folder/3/folder4/" in your compiling flags.