当前位置: 首页 > 工具软件 > XMake > 使用案例 >

xmake 入门到应用

谭昊乾
2023-12-01

环境:ubuntu16.04+xmakev2.7.2+mingw-w64

why xmake?

  • 支持跨平台(无win编译选项)
  • 支持C++库管理
  • 上手快(基于lua相比cmake语法)

下载安装

  •  此处略过

跨平台 linux-window

命令:xmake f -p PLAT -a ARCH

  • linux 64位:         xmake f -p linux -a x86_64
  • linux 32位:         xmake f -p linux -a i386

Why mingw-64?

  • 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>

交叉编译

  •         待补充

C++库管理

  • 库管理命令 xrepo
  • 库管理的三种方式
  1. 官方自带xmake-repo

    安装库:xrepo install spdlog

    工程添加库:add_requires("spdlog")

            补充:如link阶段包库不存在,检查是否未调用add_requires

    Target链接库:add_packages("spdlog")

  2. 自建my-repo(待补充)
  3. 私有third_party

    第一类: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()

    还有很多种。。。

Sample工程

  • 目录结构
  • 待整理后上传
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
 类似资料: