xmake

(7 mins to read)

XMake是一个基于Lua的现代化C/C++构建系统。

XMake = Build backend + Project generator + Package manager (包管理器是相比于CMake最大的优势)

Install

1
2
3
4
curl -fsSL https://xmake.io/shget.text | bash
xmake --version
xmake update
xmake uninstall

Use

1
2
3
4
xmake create hello
cd hello
xmake [build]
xmake run

xmake.lua:

1
2
3
4
add_rules("mode.debug", "mode.release")
target("hello")
set_kind("binary")
add_files("src/main.cpp")

Commands

1
2
3
4
5
6
7
8
9
10
11
12
xmake create
xmake config
xmake build
xmake install
xmake run
xmake clean

xmake config -m debug # debug mode
xmake config --toolchain=clang
xmake project -k compile_commands
xmake project -k cmake
xmake -v # verbose output

Code

常用选项:

1
2
3
4
5
6
7
8
9
10
11
add_cxxflags("-fno-strict-aliasing", "-fno-implicit-templates")
set_languages("c11", "cxx20") -- 设置计标准
add_includedirs("include") -- 添加头文件搜索目录
add_linkdirs("ext/lib") -- 添加库搜索目录
add_links("myext") -- 添加链接库
add_defines("MYMACRO=hello")
set_warnings("all")
is_plat()
is_arch()
is_host()
is_mode()

main + 静态库示例:

1
2
3
4
5
6
7
8
9
10
add_rules("mode.debug", "mode.release")
set_languages("cxx20")
target("mylib")
set_kind("static")
add_includedirs("include", {public = true})
add_files("src/mylib.cpp")
target("myexe")
set_kind("binary")
add_files("src/myexe.cpp")
add_deps("mylib", {inherit = false}) -- 可以继承目标依赖的配置信息,默认的private不会被继承,public会被继承,interface则只作用于继承的子target,而不作用与当前target

main + 动态库示例:(和静态库几乎一样,只需要改下lib的kind即可)

1
2
3
4
5
6
7
8
9
10
11

target("foo")
set_kind("shared")
add_files("src/interface.cpp")
add_defines("TEST", {public = true})
add_includedirs("src", {interface = true})

target("foo_demo")
set_kind("binary")
add_deps("foo")
add_files("src/main.cpp")

第三方库依赖:(默认安装在~/.xmake目录下)

  • xrepo search/info/fetch/install/remove <package>
  • xrepo clean
  • xrepo scan
1
2
3
4
5
6
add_rules("mode.debug", "mode.release")
add_requires("imgui 1.88", {configs = {glfw_opengl3 = true}})
target("imgui-demo")
set_kind("binary")
add_files("src/main.cpp")
add_packages("imgui")
1
add_requires("boost", {configs = {filesystem = true, serialization = true, ...}})

自定义选项:

1
2
3
4
5
6
7
8
9
10
11
option("myopt")
set_showmenu(true)
set_description("The test config option")
option_end()

target("test")
set_kind("binary")
add_files("src/*.cpp")
if is_config("myopt", "hello") then
add_defines("HELLO")
end

内置变量:

1
2
3
$(projectdir)
$(buildir)
$(curdir)