- 支持跨平台(无win编译选项)
- 支持C++库管理
- 上手快(基于lua相比cmake语法)
命令:xmake f -p PLAT -a ARCH
- mingw 是 GCC 的window版
- 结合 mingw-64 可以在linux中编译出 window可执行程序 exe
- 依赖库从mingw相关路径拷贝
- linux下都有,只是库名都是小写(报错提示一般是有大写字母)
- 别拷贝库名直接locate搜or百度下载win版
- 遇到问题
- error: ‘mutex’ in namespace ‘std’ does not name a type
- 原因:“线程模型:win32”中不支持互斥。
- 处理:必须选择任何具有“线程模型:posix”的工具链
$ sudo update-alternatives --config i686-w64-mingw32-gcc <choose i686-w64-mingw32-gcc-posix from the list> $ sudo update-alternatives --config i686-w64-mingw32-g++ <choose i686-w64-mingw32-g++-posix from the list> $ sudo update-alternatives --config x86_64-w64-mingw32-gcc <choose x86_64-w64-mingw32-gcc-posix from the list> $ sudo update-alternatives --config x86_64-w64-mingw32-g++ <choose x86_64-w64-mingw32-g++-posix from the list>
安装库:xrepo install spdlog
工程添加库:add_requires("spdlog")
补充:如link阶段包库不存在,检查是否未调用add_requires
Target链接库:add_packages("spdlog")
第一类:head-only
package("mylib") set_kind("library", {headeronly = true}) set_homepage("xxx") set_description("测试") on_load(function (package) --package:set("installdir", path.join(os.scriptdir(), package:plat(), package:arch(), package:mode())) --快速调试用 end) on_install("windows", "mingw", "linux", "cross", function (package) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include"), package:installdir()) end) package_end()
第二类:head&so
package("mylib") set_kind("library", {headeronly = false}) set_homepage("xxx") set_description("测试") on_load(function (package) --package:set("installdir", path.join(os.scriptdir(), package:plat(), package:arch(), package:mode())) --快速调试用 end) on_install("windows", "mingw", function (package) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include/mylib.h"), path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include/mylib.h")) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/lib/mylib.dll"), path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/lib/mylib.dll")) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include"), package:installdir()) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/lib"), package:installdir()) end) on_install("linux", "cross", function (package) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include/mylib.h"), path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include/mylib.h")) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/lib/mylib.so"), path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/lib/libmylib.so")) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/include"), package:installdir()) os.cp(path.join(os.scriptdir(), "mylib/$(plat)/$(arch)/lib"), package:installdir()) end) package_end()
第三类:cmake
package("mqtt") set_homepage("http://www.eclipse.org/paho/") set_description("MQTT是一种轻量级的发布/订阅消息传递协议") --add_urls("https://github.com/eclipse/paho.mqtt.c/archive/refs/tags/v$(version).tar.gz", --"https://github.com/eclipse/paho.mqtt.c.git") --add_versions("1.3.11", "c70db96e66adacae411d5d875fbb08bca6ff9945de3d215b3af93cbd22792db5") -- sha256sum v1.3.10.tar.gz --set_sourcedir(path.join(os.scriptdir(), "paho.mqtt.c-1.3.10")) -- 此版本存在2个问题: Poll 和 Use_select 编译错误 set_sourcedir(path.join(os.scriptdir(), "paho.mqtt.c-1.3.11")) -- 官方修复: Use_select 编译错误 add_deps("cmake") on_load(function (package) --package:set("installdir", path.join(os.scriptdir(), package:plat(), package:arch(), package:mode())) end) on_install("windows", "mingw", function (package) local configs = {"-DPAHO_USE_SELECT=ON", "-DPAHO_BUILD_STATIC=ON", "-DPAHO_BUILD_SHARED=OFF", "-DPAHO_BUILD_SAMPLES=OFF", "-DPAHO_ENABLE_TESTING=OFF", "-DCMAKE_VERBOSE_MAKEFILE=ON"} table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release")) --table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF")) --table.insert(configs, "-DBUILD_STATIC_LIBS=" .. (package:config("shared") and "OFF" or "ON")) import("package.tools.cmake").install(package, configs) end) on_install("linux", "cross", function (package) local configs = {"-DPAHO_BUILD_STATIC=ON", "-DPAHO_BUILD_SHARED=OFF", "-DPAHO_BUILD_SAMPLES=OFF", "-DPAHO_ENABLE_TESTING=OFF"} table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release")) --table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF")) --table.insert(configs, "-DBUILD_STATIC_LIBS=" .. (package:config("shared") and "OFF" or "ON")) import("package.tools.cmake").install(package, configs) end) package_end()
还有很多种。。。
- 目录结构
- 待整理后上传
xmake-project-demo
├── run_linux.sh
├── src
│ ├── config
│ ├── core
│ ├── demo
│ ├── main.cpp
│ └── utility
├── third_party
│ ├── mqtt.lua
│ ├── mylib
│ ├── mylib.lua
│ ├── paho.mqtt.c-1.3.11
│ ├── yaml-cpp-yaml-cpp-0.7.0
│ └── yaml.lua
└── xmake.lua