Apparently in VisualStudio 2022 there's still no easy way to add support for precompiled headers. But luckily I've stumbled upon on this interesting solution provided by GTANAdam on his Github page: https://gist.github.com/GTANAdam/3703fd5cb70d9a22159f58443a129f8b
With some minor changes I've managed to make things work for my projects.
The chmod +x precompile.sh
is needed, else you might get the error bash: ./precompile.sh: Permission denied
And instead of using the suggested sh precompile.sh
I've ended up using ./precompile.sh
. Additionally the precompile.sh
file now contains the header #!/bin/bash
(instead of #!/bin/sh
)

Sample of my precompile.sh
file:
#!/bin/bash
gch="PCH.hpp.gch"
header="PCH.hpp"
function compile
{
echo "Generating precompiled header.."
g++ -I../ -std=c++14 -c -x c++-header PCH.cpp -o PCH.hpp.gch
}
if [[ -f $gch ]]; then # File exists
if [[ $header -nt $gch ]]; then
compile
else
echo "Precompiled header already exists.."
fi
else
compile
fi
Some things to note. If you're editing your bash script file on windows platform, note that on windows, at every new line of the script, the text editors might add CR LF instead of just LF, and Linux bash might start dropping errors like: : not foundsh: 2: precompile.sh: