0%

用premake维护一个Linux C++项目

在项目根目录中创建一个文件 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
workspace "CppServer"    -- 解决方案名称
architecture "x86_64" -- 架构

configurations -- 配置文件
{
"Debug",
"Release",
"Dist"
}

flags
{
"MultiProcessorCompile"
}

outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}"

IncludeDir = {}
IncludeDir["yaml_cpp"] = "/usr/local/include/yaml-cpp"
IncludeDir["boost"] = "/usr/include/boost"

include "CppServer"
include "Test"

假设解决方案名称为**CppServer**,其中有两个项目,第一个**CppServer** 是生成静态链接库供第二个项目Test使用

项目结构如下,每一个子项目目录下都有一个premake5.lua

1
2
3
4
5
6
7
8
CppServer
├── CppServer
│ ├── ...
│ └── premake.lua
├── Test
│ ├── ...
│ └── premake.lua
└── premake.lua

CppServer工程下的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
project "CppServer"
kind "StaticLib"
language "C++"
cppdialect "C++17"
staticruntime "off"

targetdir ("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}")
objdir ("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}")

-- pchheader "cnpch.h"
-- pchsource "src/cnpch.cpp"

files
{
"src/**.h",
"src/**.cpp",
}

includedirs
{
"src",
"%{IncludeDir.yaml_cpp}",
"%{IncludeDir.boost}",
}

links
{
"yaml-cpp",
"boost"
}

defines
{
-- "_CRT_SECURE_NO_WARNINGS"
}

filter "system:windows"
systemversion "latest"

defines
{
"CS_PLATFORM_LINUX",
}

filter "configurations:Debug"
defines "CS_DEBUG"
runtime "Debug"
symbols "on"

filter "configurations:Release"
defines "CS_RELEASE"
runtime "Release"
optimize "on"

filter "configurations:Dist"
defines "CS_DIST"
runtime "Release"
optimize "on"

Test工程下的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
project "Test"
kind "ConsoleApp"
language "C++"
cppdialect "C++17"
staticruntime "off"

targetdir ("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}")
objdir ("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}")

files
{
"src/**.h",
"src/test.cpp",
}

includedirs
{
"%{wks.location}/CppServer/src",
}

links --链接库
{
"CppServer"
}

filter "system:linux"
systemversion "latest"

defines
{
"CS_PLATFORM_LINUX",
}

filter "configurations:Debug"
defines "CS_DEBUG"
runtime "Debug"
symbols "on"

filter "configurations:Release"
defines "CS_RELEASE"
runtime "Release"
optimize "on"

filter "configurations:Dist"
defines "CS_DIST"
runtime "Release"
optimize "on"

完成配置之后,在根目录使用premake生成**Makefile**

1
premake5 --os=linux gmake

再使用make生成结果文件

1
make config=debug Test

可配置选项可用 make help 查看