For the current version of premake v5. 0.0-beta1, you must do the following in order for the precompiled header to work across all IDEs (especially for Visual Studio):
Put both pch.h
and pch.cpp
under the root directory of your project (not solution/workspace).
Set the exact name (not a path) of the pch.h to pchheader()
.
Set the full path of pch.cpp
relative to premake5.lua script in pchsource()
. This is IMPORTANT. I don't know why, but if you do not specify the full relative path, then premake will Use (/Yu) (use precompiled header) to the phc.cpp
instead of Create (/Yc) (create precompiled header), which results in not creating the precompiled header in Visual Studio.
Include the directory where pch.h
and pch.cpp
are located in includedirs()
#include "pch.h"
IN EVERY .cpp file in your project, IN THE FIRST LINE of each .cpp file. Remember: #include "pch.h"
, "pch.h" must be excactly the same as the string you set in pchheader()
in your premake5.lua script.
If you have .c files, you MUST rename them to .cpp instead, otherwise Visual Studio will complain about using a precompiled header that was compiled using a c++ compiler.
I know it's overcomplicated but this is how it is.
Example:
project "LearnGL"
location "Projects/LearnGL/"
kind "ConsoleApp"
language "C++"
targetdir "builds/"
objdir "obj/%{prj.name}_%{cfg.shortname}"
pchheader "pch.h" --Do not use paths here. Use directly the header file name
pchsource "Projects/LearnGL/src/pch.cpp" --Full path MUST be specified relative to the premake5.lua (this) script.
files {
"Projects/LearnGL/src/**.h",
"Projects/LearnGL/src/**.hpp",
"Projects/LearnGL/src/**.cpp"
}
includedirs {
"Projects/LearnGL/src/", --This is where pch.h and pch.cpp are located.
"Projects/LearnGL/src/external/glad/include/",
"Projects/LearnGL/src/external/stb_image/include/",
"External/glfw/include/",
"External/spdlog/include/"
}