跳转到内容

编译配置 ​

通过xmake f|config配置命令,设置构建前的相关配置信息,详细参数选项,请运行: xmake f --help。

关于在 xmake.lua 中进行项目配置的详细说明,请参阅项目配置指南。

注意

你可以使用命令行缩写来简化输入,也可以使用全名,例如:
xmake f 或者 xmake config.
xmake f -p linux 或者 xmake config --plat=linux.

切换平台 ​

主机平台 ​

sh
$ xmake

注意

Xmake 将会自动探测当前主机平台,默认自动生成对应的目标程序。

Linux ​

sh
$ xmake f -p linux [-a i386|x86_64]
$ xmake

Android ​

sh
$ xmake f -p android --ndk=~/files/android-ndk-r10e/ [-a armeabi-v7a|arm64-v8a]
$ xmake

如果要手动指定 ndk 中具体某个工具链,而不是使用默认检测的配置,可以通过 --bin 来设置,例如:

sh
$ xmake f -p android --ndk=~/files/android-ndk-r10e/ -a arm64-v8a --bin=~/files/android-ndk-r10e/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin

--bin 主要用于设置选择编译工具的具体 bin 目录,这个的使用跟交叉编译中的 --bin 的行为是一致的。

注意

如果手动设置了 bin 目录,没有通过检测,可以看下是否 --arch= 参数没有匹配对。

iPhoneOS ​

sh
$ xmake f -p iphoneos [-a armv7|armv7s|arm64|i386|x86_64]
$ xmake

由于 m1 设备上模拟器也支持 arm64 架构,因此之前单纯从 arch 去区分是否为模拟器,已无法满足需求。 因此,2.6.5 版本,我们新增了一个参数配置去区分是否为模拟器目标。

sh
$ xmake f -p iphoneos --appledev=simulator
$ xmake f -p watchos --appledev=simulator
$ xmake f -p appletvos --appledev=simulator

Mac Catalyst ​

我们也可以指定构建 Mac Catalyst 程序。

sh
$ xmake f --appledev=catalyst

Windows ​

sh
$ xmake f -p windows [-a x86|x64]
$ xmake

Mingw ​

xmake 除了支持 Msys2/MingW, MingW for macOS/linux 之外,还支持 llvm-mingw 工具链,可以切换 arm/arm64 架构来编译。

sh
$ xmake f -p mingw --sdk=/usr/local/i386-mingw32-4.3.0/ [-a i386|x86_64|arm|arm64]
$ xmake

Apple WatchOS ​

sh
$ xmake f -p watchos [-a i386|armv7k]
$ xmake

Wasm (WebAssembly) ​

此平台用于编译 WebAssembly 程序(内部会使用emcc工具链),在切换此平台之前,我们需要先进入 Emscripten 工具链环境,确保 emcc 等编译器可用。

sh
$ xmake f -p wasm
$ xmake

xmake 也支持 Qt for wasm 编译,只需要:

sh
$ xmake f -p wasm [--qt=~/Qt]
$ xmake

其中 --qt 参数设置是可选的,通常 xmake 都能检测到 qt 的 sdk 路径。

需要注意的一点是,Emscripten 和 Qt SDK 的版本是有对应关系的,不匹配的版本,可能会有 Qt/Wasm 之间的兼容问题。

关于版本对应关系,可以看下:https://wiki.qt.io/Qt_for_WebAssembly

更多详情见:https://github.com/xmake-io/xmake/issues/956

除了 emscripten 以外,还有一个常用的 wasm 工具链 wasi-sdk,用于构建基于 wasi 的程序,我们仅仅只需要切换工具链即可。

sh
$ xmake f -p wasm --toolchain=wasi
$ xmake

HarmonyOS (鸿蒙) ​

2.9.1 版本新增了鸿蒙 OS 平台的 native 工具链编译支持:

sh
$ xmake f -p harmony

xmake 会自动探测默认的 SDK 路径,当然我们也可以指定 Harmony SDK 路径。

sh
$ xmake f -p Harmony --sdk=/Users/ruki/Library/Huawei/Sdk/openharmony/10/native

工作目录与构建目录 ​

xmake 涉及三个目录,平时它们是同一个,所以很少需要区分:

目录是什么怎么指定
工程目录根 xmake.lua 所在的目录-P/--project 或 -F/--file
工作目录xmake 运行的当前目录,.xmake 配置缓存放在这里就是当前目录
构建目录构建产物的输出目录-o/--buildir

默认:工作目录就是工程目录 ​

这是一直以来的行为,在工程根目录下直接运行 xmake,build 和 .xmake 都生成在工程里:

projectdir (工作目录)
├── xmake.lua
├── src
├── build     (生成)
└── .xmake    (生成)
sh
$ cd projectdir
$ xmake

把构建目录放到别处 ​

-o 只改变产物的输出位置,工作目录仍然是工程根目录:

projectdir (工作目录)
├── xmake.lua
├── src
└── .xmake    (生成)
build         (生成)
sh
$ cd projectdir
$ xmake f -o ../build
$ xmake

外部工作目录 ​

用 -P 指定工程目录之后,当前目录就成为独立的工作目录,工程目录一个字都不会被写入,适合源码目录只读、或者想让同一份源码有多个并行构建的场景:

workdir
├── build     (生成)
└── .xmake    (生成)
projectdir
├── xmake.lua
└── src
sh
$ cd workdir
$ xmake f -P ../projectdir
$ xmake

两个都放到外面也可以:

sh
$ cd workdir
$ xmake f -P ../projectdir -o ../build

注意

-P 指定的工程目录会被记住。之后在这个工作目录里直接运行 xmake、xmake run 等命令, 不用再带 -P,构建的仍然是之前配置的那个工程。这个绑定保存在 .xmake 里,是外部工作目录模式 的设计本意。

解除工程绑定 ​

如果当前工作目录本身也是一个工程(它自己有 xmake.lua),而之前又在这里绑定过别的工程, 那么直接运行 xmake 构建的会是被绑定的那个,而不是本地的。这种情况下 xmake 会给出提示:

warning: we are building the project(/path/to/other) which has been configured in this directory,
it shadows the xmake.lua of this directory, please run `xmake f -P .` to build that one instead.

按提示重新绑定到当前目录即可:

sh
$ xmake f -P .

也可以直接删掉 .xmake 目录,恢复到默认行为。

全局配置 ​

我们也可以将一些常用配置保存到全局配置中,来简化频繁地输入:

例如:

sh
$ xmake g --ndk=~/files/android-ndk-r10e/

现在,我们重新配置和编译android程序:

sh
$ xmake f -p android
$ xmake

以后,就不需要每次重复配置 --ndk= 参数了。

注意

每个命令都有其简写,例如: xmake g 或者 xmake global.

清除配置 ​

有时候,配置出了问题编译不过,或者需要重新检测各种依赖库和接口,可以加上 -c 参数,清除缓存的配置,强制重新检测和配置

sh
$ xmake f -c
$ xmake

或者:

sh
$ xmake f -p iphoneos -c
$ xmake

导入导出配置 ​

2.5.5 之后,我们还可以导入导出已经配置好的配置集,方便配置的快速迁移。

导出配置 ​

sh
$ xmake f --export=/tmp/config.txt
$ xmake f -m debug --xxx=y --export=/tmp/config.txt

导入配置 ​

sh
$ xmake f --import=/tmp/config.txt
$ xmake f -m debug --xxx=y --import=/tmp/config.txt

导出配置(带菜单) ​

sh
$ xmake f --menu --export=/tmp/config.txt
$ xmake f --menu -m debug --xxx=y --export=/tmp/config.txt

导入配置(带菜单) ​

sh
$ xmake f --menu --import=/tmp/config.txt
$ xmake f --menu -m debug --xxx=y --import=/tmp/config.txt