使用premake生成项目

Visual Studio 进行项目配置较为复杂,利用premake可快速生成,premake使用lua编写配置文件,编写方便,语法简单,跨平台.
开始之前需要下载premake5.exe可执行文件,项目地址为https://github.com/premake
下面使用premake生成一个OpenGL项目.
项目目录结构如下


在项目根目录下新建一个premake5.lua文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--解决方案名称
workspace "OpenGL-Sandbox"
--架构
architecture "x64"
--启动项目名称
startproject "OpenGL-Sandbox"
--构建类型
configurations
{
"Debug",
"Release"
}
--指定编译或链接过程的标记
flags
{
--多核并行编译
"MultiProcessorCompile"
}
--全局变量,指定输出的目录
outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}"

--定义子项目的位置
IncludeDirs = {}
IncludeDirs["GLFW"] = "vendor/GLFW"
IncludeDirs["Glad"] = "vendor/Glad"
IncludeDirs["imgui"] = "vendor/imgui"

--包含子项目,子项目中的premake5.lua会继承解决方案的premake5.lua中的全局变量
include "vendor/GLFW"
include "vendor/Glad"
include "vendor/imgui"

--下面定义启动项目OpenGl-Sandbox
project "OpenGL-Sandbox"
kind "ConsoleApp" --项目类型,此处为命令行项目
language "C++" -- 语言 c++
cppdialect "C++17" --c++版本

targetdir ("./bin/" .. outputdir .. "/%{prj.name}") --生成的目录 prj是premake内置的变量,表示项目对象
objdir ("./bin-int/" .. outputdir .. "/%{prj.name}") --中间文件目录 prj是premake内置的变量,表示项目对象

files --项目的源文件
{
"src/**.h",
"src/**.cpp",
"src/**.c",
"vendor/stb_image/**.h",
"vendor/stb_image/**.cpp",
"vendor/glm/**.hpp",
"vendor/glm/**.inl",
}

includedirs --项目包含的目录
{
"src",
"vendor",
"vendor/GLFW/include",
"vendor/Glad/include",
"vendor/imgui",
"vendor/glm",
"vendor/stb_image"
}


links { --链接的静态库
"Glad",
"GLFW",
"ImGui"
}

编写完解决方案的premake配置后,开始编写子项目的premake配置,下面以GLFW为例,在vendor/GLFW下新建premake5.lua文件.内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
project "GLFW"
kind "StaticLib" --项目类型为静态库
language "C" --语言为C

files --项目的源文件
{
"%{IncludeDir.GLFW}/include/GLFW/glfw3.h",
"%{IncludeDir.GLFW}/include/GLFW/glfw3native.h",
"%{IncludeDir.GLFW}/src/glfw_config.h",
"%{IncludeDir.GLFW}/src/context.c",
"%{IncludeDir.GLFW}/src/init.c",
"%{IncludeDir.GLFW}/src/input.c",
"%{IncludeDir.GLFW}/src/monitor.c",
"%{IncludeDir.GLFW}/src/vulkan.c",
"%{IncludeDir.GLFW}/src/window.c"
}

filter "system:windows" --筛选器 定义windows系统的配置
systemversion "latest" --系统版本
staticruntime "On" --设置RuntimeLibrary,on时动态库也会生成lib,dll被编译进exe

files --项目源文件
{
"%{IncludeDir.GLFW}/src/win32_init.c",
"%{IncludeDir.GLFW}/src/win32_joystick.c",
"%{IncludeDir.GLFW}/src/win32_monitor.c",
"%{IncludeDir.GLFW}/src/win32_time.c",
"%{IncludeDir.GLFW}/src/win32_thread.c",
"%{IncludeDir.GLFW}/src/win32_window.c",
"%{IncludeDir.GLFW}/src/wgl_context.c",
"%{IncludeDir.GLFW}/src/egl_context.c",
"%{IncludeDir.GLFW}/src/osmesa_context.c"
}

defines --定义宏
{
"_GLFW_WIN32",
"_CRT_SECURE_NO_WARNINGS"
}

filter "configurations:Debug" --Debug模式的配置
runtime "Debug" --运行时
symbols "on" --开启pdb文件

filter "configurations:Release" --Release模式
runtime "Release"
optimize "on" --开启优化


includedirs{ --包含目录
"%{IncludeDir.GLFW}/include", --IncludeDir 为上面解决方案配置文件中定义的全局变量
}
targetdir ("./bin/" .. outputdir .. "/%{prj.name}") --生成目录
objdir ("./bin-int/" .. outputdir .. "/%{prj.name}") --中间文件目录

imgui,Glad项目配置类似. 都编写好后,运行premake5.exe vs2019 就会生成vs解决方案