How to precompile <bits/stdc++.h> in ubuntu 20.04 LTS--HELP?

I switched to Ubuntu and I am having problem in precompiling bits/stc++.h header file as specified in a codeforces blog I did the same but at last step that is creation of precompiled header file in specified location it’s showing an error:
fatal error: can’t create precompiled header stdc++.h.gch: Permission denied

I am not sure what to do…
Please help me…

Share more information (code, screenshots etc).

PS: I also use Ubuntu, I didn’t face any issues. :wink:

here is the screenshot

How about sudo g++ -std=c++14 stdc++.h ? You are making a change in ‘/’ directory so you need to have root privileges.


Key observations:

  • The current working directory is /usr/include/x86_64-linux-gnu/c++/9/bits - Which is not needed. You can compile your C++ codes from the home directory itself.
  • You are trynna use the c++14 standard to compile - This is not needed for CP. You can use the g++ compiler to compile and run your code unless you are too specific about the compiler standard/version.

You can try doing this.

Open the terminal and navigate to the directory where your CPP scripts are present.
Use the following commands to compile

# Compile using g++ compiler
g++ script.cpp
# Execute the compiled script

# Note that it will read the input from stdin
# so you have to type the input and hit enter so it reads it
# The output will be displayed on the terminal itself.
# Compile using g++ compiler but store the object file with a different name
g++ -o hello_world script.cpp

# or
g++ -o factorial prog1.cpp
# Read input from a file and write output to another file
# Say input.txt has sample input and you
# want to write the output to output.txt

g++ -o factorial prog1.cpp
./factorial < input.txt > output.txt

If you are still confused and need help, you can post a comment.

1 Like

I think what he is asking is " how to precompile a bit/stdc++.h header " not “how to run a CPP file” :thinking:


Um, yeah and I overlooked :grimacing:

Good username/ post combo XD


:grin: :grin:

1 Like

Thank you very much It worked…

1 Like

I didn’t know to run a cpp file like this, thankyou for sharing this…

All you have to do is put a “sudo” in front of the command.
Your command :- g++ …(your compiling flags) stdc++.h
Ouput :- fatal error: can’t create precompiled header stdc++.h.gch: Permission denied

Correct command :- sudo g++ …(your compiling flags) stdc++.h

Make sure you get a new file in the same directory as stdc++.h :- “stdc++.h.gch”

Then you’re good to go.