premake-core icon indicating copy to clipboard operation
premake-core copied to clipboard

Can't create a precompiled header for Visual Studio 2019

Open ClementDreptin opened this issue 2 years ago • 10 comments

What are you trying to do? I'm trying to create a precompiled header for Visual Studio 2019.

What problem are you having? My pchheader is set correctly but my pchsource is not. When I right click on the pch source file and go to Properties > Configuration Properties > C/C++ > Precompiled Headers, my source file is set to Use (/Yu) instead of Create (Yc).

What have you tried so far? I made minimal example to replicate my issue. My project structure is like so:

MyProject/
    src/
        main.cpp
        pch.cpp
        pch.h
PremakeConfig/
    main.lua
premake.5.exe

My premake config is as follows:

workspace "MyWorkspace"
    location ".."
    startproject "MyProject"

    configurations { "Debug", "Release" }

OutputDir = "%{cfg.buildcfg}"
TargetDir = "%{wks.location}/bin/%{OutputDir}"
ObjDir = "%{wks.location}/bin-int/%{OutputDir}"

project "MyProject"
    kind "ConsoleApp"
    location "%{wks.location}/%{prj.name}"
    language "C++"

    targetdir "%{TargetDir}/%{prj.name}"
    objdir "%{ObjDir}/%{prj.name}"

    pchheader "pch.h"
    pchsource "%{prj.location}/src/pch.cpp"

    files {
        "%{prj.location}/src/**.h",
        "%{prj.location}/src/**.cpp"
    }

    includedirs "%{prj.location}/src"

I ran the command .\premake5.exe --file="PremakeConfig\main.lua" vs2019.

Explicitly adding every file in files like so:

files {
    "%{prj.location}/src/pch.h",
    "%{prj.location}/src/pch.cpp",
    "%{prj.location}/src/main.cpp"
}

solves the problem. The problem is that the real project I'm working on has a lot of files and I don't wan't to list them all in my config and have to change it every time I add a new file.

Is this a bug or is there something I'm not understanding about precompiled headers?

What version of Premake are you using? premake5 (Premake Build Script Generator) 5.0.0-alpha15

Thanks in advance.

Clément

ClementDreptin avatar Jul 15 '21 15:07 ClementDreptin

After further investigation, I realized the problem came from the prj.location token. I replaced it with ../%{prj.name} (which is what prj.location is in my case) and it worked. I also tried %{wks.location}/%{prj.name} but it didn't work so the problem might come from wks.location.

ClementDreptin avatar Jul 16 '21 13:07 ClementDreptin

Just a sanity check on this, are you intentionally setting the location to be the parent directory?. Also, should:

pchheader "pch.h"

be

pchheader "%{prj.location}/src/pch.h"

nickclark2016 avatar Jul 27 '21 02:07 nickclark2016

Yes, I'm intentionally setting the location to be the parent directory. Why can't pchheader just be pch.h if %{prj.location}/src is in includedirs?

ClementDreptin avatar Jul 27 '21 06:07 ClementDreptin

Yes, I'm intentionally setting the location to be the parent directory. Why can't pchheader just be pch.h if %{prj.location}/src is in includedirs?

That would make sense if you could, just trying to grab information. I'm not very familiar with PCH, so I'll try to look into it more this weekend. Is this in a public repo or anything that I can pull to immediately test out this issue?

nickclark2016 avatar Jul 27 '21 12:07 nickclark2016

The small example is not in a repo. I attached a zip archive with all the files you need to test to this comment. You'll just have to run the batch file to generate the .sln and .vcxproj files.

PchIssue.zip

ClementDreptin avatar Jul 27 '21 12:07 ClementDreptin

Appreciated. I'll try to take a look over the coming weekend.

nickclark2016 avatar Jul 27 '21 12:07 nickclark2016

Thanks for your time :)

ClementDreptin avatar Jul 27 '21 13:07 ClementDreptin

In case you haven't seen it already, this page in the user guide explains some hoops to jump through to get PCH working across tooling environments. Not sure if it actually applies to your case.

starkos avatar Jul 27 '21 17:07 starkos

After further investigation, I realized the problem came from the prj.location token. I replaced it with ../%{prj.name} (which is what prj.location is in my case) and it worked. I also tried %{wks.location}/%{prj.name} but it didn't work so the problem might come from wks.location.

I have been having this same issue. I came to this same conclusion, and it is consistently reproducible. It seems the the location token doesn't expand correctly when used in the pchsource() command. Neither wks nor prj work in this instance. When using the tokens for location the .vcxproj file will not contain the expected value of:

<ClCompile Include="pch.cpp">
    <PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>

More specifically, it will only create the line <ClCompile Include="pch.cpp" />

It's also worth noting, I am using 5.0.0alpha16 on VStudio2019

GroverErin avatar Sep 24 '21 03:09 GroverErin

Is there any update on this? I have the same problem

Drischdaan avatar Dec 14 '23 19:12 Drischdaan