添加precompiled header
#pragma once
#include<iostream>
#include<memory>
#include<utility>
#include<algorithm>
#include<functional>
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#ifdef HZ_PLATFORM_WINDOWS
#include<Windows.h>
#endif // HZ_PLATFORM_WINDOWS

修改premake5.lua
project "Hazel"
location "Hazel"
kind "SharedLib" --dll
language "C++"
targetdir("bin/" .. outputDir .. "/%{prj.name}")
objdir("bin-int/" .. outputDir .. "/%{prj.name}")
pchheader "hzpch.h"
pchsource "Hazel/src/hzpch.cpp"
files
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp",
}
includedirs
{
"%{prj.name}/vendor/spdlog/include",
"%{prj.name}/src",
}
filter "system:windows"
cppdialect "C++17"
staticruntime "On" --linking the runtime library
systemversion "latest"
defines
{
"HZ_BUILD_DLL",
"HZ_PLATFORM_WINDOWS",
}
--create a postbuild step to put the .dll where we want to be
postbuildcommands{
("{COPY} %{cfg.buildtarget.relpath} ../bin/" ..outputDir.. "/Sandbox")
}
filter "configurations:Debug" --only apply to Debug configurations
defines "HZ_DEGUG"
symbols "On"
filter "configurations:Release" --only apply to Debug configurations
defines "HZ_RELEASE"
optimize "On"
filter "configurations:Dist" --only apply to Debug configurations
defines "HZ_DIST"
optimize "On"
pchheader "hzpch.h"
pchsource "Hazel/src/hzpch.cpp"
重新运行GenerateProject.bat

可以看到项目中的precompiled header已经修改了

把所有的.cpp文件都 include "hzpch.h"
Application.cpp
#include "hzpch.h"
#include "Application.h"
#include "Hazel/Event/ApplicationEvent.h"
#include "Hazel/Log.h"
namespace Hazel {
Application::Application()
{
}
Application::~Application()
{
}
void Application:: Run() {
WindowResizeEvent e(1280, 720);
HZ_TRACE(e);
while (true);
}
}
Log.cpp
#include "hzpch.h"
#include "Log.h"
namespace Hazel {
std::shared_ptr<spdlog::logger> Log::s_CoreLogger; //static 对象需要在类外初始化
std::shared_ptr<spdlog::logger> Log::s_ClientLogger;
void Log::Init() {
spdlog::set_pattern("%^[%T] %n: %v%$");
s_CoreLogger = spdlog::stdout_color_mt("HAZEL");
s_CoreLogger->set_level(spdlog::level::trace);
s_ClientLogger = spdlog::stderr_color_mt("APP");
s_ClientLogger->set_level(spdlog::level::trace);
}
}
查看编译时间:

使用预编译头(precompiled header)时间

不使用预编译头时间:
可以看到添加了precompiled header会更加快
博客介绍了添加预编译头的操作步骤,包括添加precompiled header、修改premake5.lua文件,重新运行GenerateProject.bat,将所有.cpp文件包含“hzpch.h”。通过对比使用和不使用预编译头的编译时间,表明添加预编译头可加快编译速度。
600

被折叠的 条评论
为什么被折叠?



