Skip to content

Instantly share code, notes, and snippets.

@GTANAdam
Last active February 9, 2023 13:25
Show Gist options
  • Select an option

  • Save GTANAdam/3703fd5cb70d9a22159f58443a129f8b to your computer and use it in GitHub Desktop.

Select an option

Save GTANAdam/3703fd5cb70d9a22159f58443a129f8b to your computer and use it in GitHub Desktop.
Using Precompiled headers for Linux (WSL) development in Visual Studio
#!/bin/sh
gch="src/stdafx.h.gch"
header="src/stdafx.h"
function compile
{
echo "Generating precompiled header.."
# IMPORTANT: Make sure to edit this so it has all the required flags!
g++ -c -x c++-header stdafx.cpp -o stdafx.h.gch
}
if [[ -f $gch ]]; then # File exists
if [[ $header -nt $gch ]]; then
compile
else
echo "Precompiled header already exists.."
fi
else
compile
fi
Precompiled headers basically improve build times by reducing the amount of code compiling each time a build
command is invoked.
This feature is available in Visual Studio for Windows C/C++ development
via project "Properties" -> "C/C++" -> "Precompiled Headers",
but this feature is missing for Linux C/C++ Development (Visual Studio) and
requires a workaround since GCC/Clang support pch.
The workaround:
- Creating a "precompile.sh" bash script and appending the content above.
- Set in your project's properties in "Properties" -> "Build Events" -> "WSL Pre-Build Event", the following:
1. Command Line: sh precompile.sh
2. Use In Build: Yes
This will basically launch every time a build command is invoked, the header file date will be checked,
if the header is newer than the pch (precompiled header) or the pch is missing, it will (re)compile the header and
continue with the build process.
Credits: https://developercommunity.visualstudio.com/t/linux-builds-add-precompiled-headers-support/569226
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment