Toolchains & Project Generators
xmake knows many compiler families by name (GCC, Clang, MSVC, clang-cl, Zig's zig cc, cross GCCs, Android NDK, Emscripten, CUDA, and more) as toolchains. Switching is one configuration flag; there is no cross file to write for the common cases.
Switching between GCC and Clang
mkdir -p tc && cd tc
cat > xmake.lua <<'L'
add_rules("mode.debug", "mode.release")
target("hello")
set_kind("binary")
add_files("main.c")
L
cat > main.c <<'SRC'
#include <stdio.h>
int main(void) {
#if defined(__clang__)
printf("built by clang %d\n", __clang_major__);
#else
printf("built by gcc %d\n", __GNUC__);
#endif
return 0;
}
SRC
xmake -j1 > /dev/null 2>&1 && xmake run hello
echo "--- switch to clang (-c clears the cached configuration)"
xmake f --toolchain=clang -c > /dev/null
xmake -j1 -v 2>&1 | grep -E '^/usr/bin/clang' | cut -c1-80
xmake run hello
echo "--- or pick individual tools"
xmake f --cc=gcc --ld=gcc -c > /dev/null
xmake -j1 > /dev/null 2>&1
xmake run hellobuilt by gcc 13
--- switch to clang (-c clears the cached configuration)
/usr/bin/clang -c -Qunused-arguments -m64 -fvisibility=hidden -O3 -DNDEBUG -o bu
/usr/bin/clang++ -o build/linux/x86_64/release/hello build/.objs/hello/linux/x86
built by clang 18
--- or pick individual tools
built by gcc 13
| Flag | Selects |
|---|---|
--toolchain=NAME |
a whole toolchain: gcc, clang, llvm, zig, msvc, clang-cl, mingw, cross, ndk, emcc… |
--cc=, --cxx=, --ld=, --ar=, --sh= |
single tools (C compiler, C++ compiler, linker, archiver, shared-library linker) |
-p PLAT -a ARCH |
platform and architecture (-p cross -a arm64 --sdk=/opt/arm-toolchain, -p android --ndk=…, -p mingw) |
set_toolchains("clang") in xmake.lua |
a default for the project or one target |
-c (--clean) discards the cached configuration so the toolchain is detected again, which is the xmake equivalent of a new build directory.
For a full cross toolchain, xmake needs the cross compiler on disk (-p cross --sdk=/opt/gcc-arm finds bin/arm-…-gcc); the course image has none, so the Meson lesson's honest "no sysroot" story applies equally here.
Generating compile_commands.json and other build files
xmake can export the project for other tools with xmake project -k KIND:
| Kind | Writes |
|---|---|
compile_commands |
compile_commands.json for clangd, clang-tidy and editors |
cmakelists |
a CMakeLists.txt |
ninja, makefile |
a build.ninja / Makefile |
vsxmake, vs2022 |
Visual Studio solutions |
xcode |
an Xcode project (through CMake) |
mkdir -p gen/src gen/include && cd gen
cat > xmake.lua <<'L'
add_rules("mode.debug", "mode.release")
target("greet")
set_kind("static")
add_files("src/greet.c")
add_includedirs("include", {public = true})
target("hello")
set_kind("binary")
add_deps("greet")
add_files("src/main.c")
L
echo 'const char *greet(void);' > include/greet.h
echo 'const char *greet(void) { return "hi"; }' > src/greet.c
echo '#include <stdio.h>
#include "greet.h"
int main(void) { puts(greet()); return 0; }' > src/main.c
xmake f -m debug > /dev/null 2>&1
xmake project -k compile_commands
python3 -c 'import json; [print(e["file"], " ".join(e["arguments"][1:])) for e in sorted(json.load(open("compile_commands.json")), key=lambda e: e["file"])]'
xmake project -k cmakelists > /dev/null
echo "--- the generated CMakeLists.txt builds with CMake"
grep -E '^(project|add_library|add_executable|target_link_libraries)' CMakeLists.txt
cmake -S . -B cmake-build -G Ninja --log-level=WARNING
cmake --build cmake-build | tail -1
./build/linux/x86_64/debug/hellocreate ok!
src/greet.c -c -m64 -g -O0 -Iinclude -o build/.objs/greet/linux/x86_64/debug/src/greet.c.o src/greet.c
src/main.c -c -m64 -g -O0 -Iinclude -o build/.objs/hello/linux/x86_64/debug/src/main.c.o src/main.c
--- the generated CMakeLists.txt builds with CMake
project(hello LANGUAGES C)
add_library(greet STATIC "")
add_executable(hello "")
target_link_libraries(hello PRIVATE
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/gen/cmake-build
[4/4] Linking C executable /home/learner/gen/build/linux/x86_64/debug/hello
hi
Two useful facts from the session: compile_commands.json reflects the current configuration (debug mode here: -g -O0), and the exported CMakeLists.txt is a snapshot, written with absolute output directories that point back into xmake's build/ tree. It is useful for IDEs that only understand CMake, or to hand a project to a CMake-only team, but it isn't meant to be maintained by hand; regenerate it whenever xmake.lua changes.
Tip:
xmake project -k compile_commandsis a one-off. To keep it up to date automatically, addadd_rules("plugin.compile_commands.autoupdate", {outputdir = "."})toxmake.lua; the VS Code xmake extension does the same.
Test yourself
How do you rebuild an xmake project with Clang instead of GCC?
- Edit every
add_filesline xmake f --toolchain=clang -c, thenxmakeCC=clang xmake run- Write a cross file
Which command writes compile_commands.json for clangd?
xmake project -k compile_commandsxmake show -cxmake f --exportxmake run clangd
Why should a generated CMakeLists.txt from xmake project -k cmakelists not be edited by hand?
- It is binary
- It is a snapshot of the current xmake configuration; changes are lost on the next export and drift from
xmake.lua - CMake refuses to read it
- It contains no targets