Search code examples
premake

premake5 precompiled headers not working


(this is using Premake5 alpha binary available for download on website)

I'm trying to port my existing VS solution over to using premake5.

It uses MS style precompiled headers(stdafx.h/stdafx.cpp).

When I specify this is my test project:

pchheader "stdafx.h"
pchsource "stdafx.cpp"

It does set the project to using precompiled headers, but it is not setting stdafx.cpp to generate precompiled headers(/Yc). Instead all the files in the project are trying to use(/Yu) and nobody is generating the PCH. So it does not build..

I'm guessing this does works somehow, what black magic am I missing here?

Here is my entire premake5 file for reference

-- premake5.lua
solution "Cloud"
   configurations { "Debug", "Release", "Final" }
   platforms { "Win32_AVX2", "Win64_AVX2"}
   location "premake"
   
flags{"MultiProcessorCompile", "ExtraWarnings", "FatalCompileWarnings", "FatalLinkWarnings", "FloatFast"}

startproject "Cloud"
vectorextensions "AVX2"

filter { "platforms:Win32" }
	system "Windows"
	architecture "x32"
filter { "platforms:Win64" }
	system "Windows"
	architecture "x64"
	
filter "configurations:Debug"
    defines { "DEBUG" }
    flags { "Symbols" }
filter "configurations:Release"
    defines { "NDEBUG" }
	flags{"Symbols"}
    optimize "Speed"
filter "configurations:Final"
    defines { "NDEBUG" }
	flags{"LinkTimeOptimization"}
    optimize "Speed"
	
group "app"

--primary executable	
project "Cloud"
	location "../src_test/cloud"
	kind "ConsoleApp"
	language "C++"
	targetdir "..//%{cfg.buildcfg}"
	pchheader "stdafx.h"
	pchsource "stdafx.cpp"
	vpaths{
		{["src/pch/*"] = "../src_test/cloud/stdafx.*"},
		{["src/*"] = "../src_test/cloud/**.cpp"},
		{["module/*"] = "../src_test/cloud/Module*.h"},
		{["core/*"] = "../src_test/cloud/Core*.h"},
		{["headers*"] = "../src_test/cloud/*.h"},
		--{["src_c/*"] = "../src_test/cloud/**.c"}
	}
	files { "../src_test/cloud/*.h", "../src_test/cloud/*.c", "../src_test/cloud/*.cpp", "../src_test/cloud/*.hpp" }	

One related question: how do I disable precompiled header usage on specific files within a project? Some of my files will not build if the PCH is included, so I have manually disabled them in the existing solution/projects.

Thanks!


Solution

  • It's probably because pchsource requires a file path (like files) Since your stdafx.cpp is not in the same directory as your script, Premake does not find it. Try using pchsource "../src_test/cloud/stdafxcpp" instead, it should fix the problem.

    Also I see that you don't add "../src_test/cloud/" as an include directory, so that means that your pch header will be included using relative path, right ? If so, you'll need to update pchheader to reflect that. Due to the way Visual Studio works, you need to set pchheader as it appears in your cpp files. E.g. if in your cpp files you have #include "../src_test/cloud/stdafx.h" you need to use this in Premake: pchheader "../src_test/cloud/stdafx.h".

    And finally, to deactivate precompiled headers on certain files, you can use filters:

    -- deactivate precompiled headers for C files
    filter "files:**.c"
        flags { "NoPCH" }