ahnaf09's blog

By ahnaf09, history, 14 months ago, In English

I am new to Ubuntu and trying to precompile the bits header for faster C++ compilation. On my system, the path to the header files is:

/usr/include/x86_64-linux-gnu/c++/11/bits

Listing the directory contents after navigating to the path shows the following: Directory Listing


Current Sublime Text Build System

Here is my current Sublime Text build configuration:

{
  "shell_cmd": "g++ -std=c++20 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wno-unused-result -DLOCAL -include ~/.stdc++.pch -o '$$${file_path}/$$${file_base_name}' '$$${file}' && timeout 10s '$$${file_path}/$$${file_base_name}'  \lt  '$$${file_path}/in.txt' > '$$${file_path}/out.txt' 2 \gt  '$$${file_path}/err.txt'",
  "file_regex": "^(...*?):([0-9]+):([0-9]+): (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c++, source.cpp",
  "variants": [
    {
      "name": "Run Only",
      "shell_cmd": "timeout 10s '$$${file_path}/$$${file_base_name}' < '$$${file_path}/in.txt'  \gt  '$$${file_path}/out.txt' 2> '${file_path}/err.txt'"
    },
    {
      "name": "Compile Only",
      "shell_cmd": "g++ -std=c++20 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wno-unused-result -DLOCAL -include ~/.stdc++.pch -o '$$${file_path}/$$${file_base_name}' '${file}'"
    }
  ]
}

Errors Encountered

After using this build system, I received compilation errors as shown in the screenshot: Compilation Error


Notes

  • The build attempts to include a precompiled header ~/.stdc++.pch.
  • Compilation fails, possibly due to incorrect precompiled header setup or incompatible paths.
  • I am looking for a proper way to precompile the bits header in Ubuntu and integrate it with Sublime Text.
  • Code Link where i error
  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
14 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

That's now how -include is supposed to work.

  1. Create some directory to store the PCH. Let's take ~/projects/pch as an example.
  2. Create a bits directory in the above dir. mkdir ~/projects/pch/bits
  3. Copy the stdc++.h file into the bits dir. cp /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h ~/projects/pch/bits
  4. cd ~/projects/pch
  5. Compile the PCH (while staying in the pch dir). g++ ... -I. -x c++-header -c ./bits/stdc++.h. This will create a ./bits/stdc++.h.gch file. Replace ... with the usual flags you use (excluding the -include ~/.stdc++.pch part).
  6. Replace -include ~/.stdc++.pch in your compilation commands with -I ~/projects/pch.

Now, #include <bits/stdc++.h> will prioritize ~/projects/pch/bits/stdc++.h and end up using the PCH.

You can test if it's working or not by running g++ ... -I ~/projects/pch -H A.cpp. -H will print all the header files used. It's working if you see something like ! /home/<your user>/projects/pch/bits/stdc++.h.gch in the command output.