4

In a regular VisualStudio project you can setup the precompiled header by going to Configuration Properties -> C/C++ -> Precompiled Headers.enter image description here

However, when dealing with a Cross Platform (Linux) project, the Precompiled Header option is missing.

enter image description here

Is there any way to configure the Precompiled Header?

Gediminas
  • 1,830
  • 3
  • 27
  • 47

1 Answers1

0

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)

enter image description here

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:

Gediminas
  • 1,830
  • 3
  • 27
  • 47