xmake常用命令

一、创建程序

示例:

xmake create -l c++ -P hello

参数说明:

  • create:创建一个新项目
  • -l c++:项目语言为c++
  • -P hello:更改到给定的项目目录,这里是hello

更多参数可以使用命令:xmake create --help 查看

使用创建模板程序

示例:

xmake create -l c++ -t console -P hello

参数说明:

  • -t console:使用控制台模版

创建静态库程序

使用模板创建

xmake create -l c++ -t static hello

创建动态库程序

使用模板创建

xmake create -l c++ -t shared  hello

二、运行程序

示例:

## 以 hello 程序为例

# 进入程序目录
cd hello

# 编译程序
xmake

# 运行程序,默认debug
xmake run

# release 编译
xmake f -m release

三、xmake.lua

xmake的工程描述文件xmake.lua虽然基于lua语法,但是为了使得更加方便简洁得编写项目构建逻辑,xmake对其进行了一层封装,使得编写xmake.lua不会像写makefile那样繁琐

基本上写个简单的工程构建描述,只需三行就能搞定,例如:

target("test")
    set_kind("binary")
    add_files("src/*.c")

更多配置语法参数:https://xmake.io/#/zh-cn/guide/syntax_description

四、包管理

4.1 xrepo

xrepo 是一个基于 Xmake 的跨平台 C/C++ 包管理器。

它基于 xmake 提供的运行时,但却是一个完整独立的包管理程序,相比 vcpkg/homebrew 此类包管理器,xrepo 能够同时提供更多平台和架构的 C/C++ 包。

并且还支持多版本语义选择,另外它还是一个去中心化的分布式仓库,不仅仅提供了官方的 xmake-repo 仓库,还支持用户自建多个私有仓库。

同时,xrepo 也支持从 vcpkg/homebrew/conan 等第三方包管理器中安装包,并提供统一一致的库链接信息,方便与第三方项目的集成对接。

如果你想要了解更多,请参考:在线文档, Github 以及 Gitee

4.2 使用 vcpkg 包

添加 vcpkg 的依赖包

add_requires("vcpkg::zlib", "vcpkg::pcre2")

target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_packages("vcpkg::zlib", "vcpkg::pcre2")

我们也可以加个包别名,简化对add_packages的使用:

add_requires("vcpkg::zlib", {alias = "zlib"})
add_requires("vcpkg::pcre2", {alias = "pcre2"})

target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_packages("zlib", "pcre2")

如果 vcpkg 包带有可选特性,我们也可以直接使用 vcpkg 的语法格式 packagename[feature1,feature2] 来安装包。

add_requires("vcpkg::boost[core]")

v2.6.3 之后,xmake 支持 vcpkg 新的清单模式,通过它,我们就能支持 vcpkg 包的版本选择,例如:

add_requires("vcpkg::zlib 1.2.11+10")
add_requires("vcpkg::fmt >=8.0.1", {configs = {baseline = "50fd3d9957195575849a49fa591e645f1d8e7156"}})
add_requires("vcpkg::libpng", {configs = {features = {"apng"}}})

target("test")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("vcpkg::zlib", "vcpkg::fmt", "vcpkg::libpng")

v2.6.8 之后,还可以额外配置私有仓库,仅清单模式有效。

local registries = {
    {
        kind = "git",
        repository = "https://github.com/SakuraEngine/vcpkg-registry",
        baseline = "e0e1e83ec66e3c9b36066f79d133b01eb68049f7",
        packages = {
            "skrgamenetworkingsockets"
        }
    }
}
add_requires("vcpkg::skrgamenetworkingsockets >=1.4.0+1", {configs = {registries = registries}})

五、杂项

常用命令、技巧收集,默认都是程序目录执行。

命令作用
xmake project -k cmakelists将程序转换为cmake管理
xmake project -k vsxmake将程序转换为 Visual Studio 管理,使用 VS 做 IDE 有代码提示,调式方便( 推荐
chcp 65001CMD 临时设置UTF-8编码
chcp 936CMD 临时设置GBK编码