xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
9.82k stars 774 forks source link

MinGW平台上cmake包编译问题 #5518

Closed 24bit-xjkp closed 2 weeks ago

24bit-xjkp commented 2 weeks ago

Xmake 版本

xmake v2.9.4+HEAD.e85b001f1

操作系统版本和架构

Windows 11 24H2

描述问题

  1. 要使用gcc工具链编译Windows上的包需要使用mingw平台(一些包不支持cross平台),但gcc工具链可以独立安装,此时系统中不存在mingw相关环境,包括mingw32-make.exe。而xmake在mingw平台上默认需要该工具。
  2. xmake内建的clang和gcc工具链的c++工具链定义中,clang和gcc的定义先于clang++和g++,这会导致在mingw平台上使用上述工具链时,在链接阶段错误的使用clang和gcc去链接c++文件,导致缺少c++标准库进而链接失败。

期待的结果

  1. 在找不到mingw时自动尝试使用ninja
  2. 调整c++编译器的定义顺序

工程配置

add_requires("benchmark")
add_packages("benchmark")

target("test")
    add_files("*.cpp")

附加信息和错误日志

  1. 找不到mingw
xmake f --toolchain=clang -p mingw -c
checking for architecture ... x86_64
checking for Microsoft Visual Studio (x64) version ... 2022
checking for Microsoft C/C++ Compiler (x64) version ... 19.41.34120
note: install or modify (m) these packages (pass -y to skip confirm)?
in xmake-repo:
  -> benchmark 1.8.5 [toolchains:"clang"]
please input: y (y/n/m)
y
  => install benchmark 1.8.5 .. failed

mingw not found!
if you want to get more verbose errors, please see:
  -> C:\Users\2283\AppData\Local\.xmake\cache\packages\2408\b\benchmark\1.8.5\installdir.failed\logs\install.txt
error: install failed!
  1. 链接器错误

注意到链接时使用的是D:\\Tools\\clang\\bin\\clang.exe而不是clang++

kind: "try_compile-v1"
    backtrace:
      - "cmake/CXXFeatureCheck.cmake:41 (try_compile)"
      - "CMakeLists.txt:308 (cxx_feature_check)"
    directories:
      source: "C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/build_9ec7839e/CMakeFiles/CMakeTmp"
      binary: "C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/build_9ec7839e/CMakeFiles/CMakeTmp"
    cmakeVariables:
      CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS: "D:/Tools/clang/bin/clang-scan-deps.exe"
      CMAKE_CXX_FLAGS: "-m64  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Wconversion  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety"
      CMAKE_CXX_FLAGS_DEBUG: "-g"
      CMAKE_EXE_LINKER_FLAGS: "-m64"
      CMAKE_MODULE_PATH: "C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/cmake/Modules;C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/cmake"
      CMAKE_OSX_SYSROOT: ""
    buildResult:
      variable: "COMPILE_HAVE_STD_REGEX"
      cached: true
      stdout: |
        Change Dir: 'C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/build_9ec7839e/CMakeFiles/CMakeTmp'

        Run Build Command(s): D:/Tools/CMake/bin/cmake.exe -E env VERBOSE=1 D:/Tools/build/bin/mingw32-make.exe -f Makefile cmTC_f92d4/fast
        D:/Tools/build/bin/mingw32-make.exe  -f CMakeFiles\\cmTC_f92d4.dir\\build.make CMakeFiles/cmTC_f92d4.dir/build
        mingw32-make[1]: Entering directory 'C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/build_9ec7839e/CMakeFiles/CMakeTmp'
        Building CXX object CMakeFiles/cmTC_f92d4.dir/std_regex.cpp.obj
        D:\\Tools\\clang\\bin\\clang.exe   -m64  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Wconversion  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -std=c++11 -MD -MT CMakeFiles/cmTC_f92d4.dir/std_regex.cpp.obj -MF CMakeFiles\\cmTC_f92d4.dir\\std_regex.cpp.obj.d -o CMakeFiles\\cmTC_f92d4.dir\\std_regex.cpp.obj -c C:\\Users\\2283\\AppData\\Local\\.xmake\\cache\\packages\\2408\\b\\benchmark\\1.8.5\\source\\cmake\\std_regex.cpp
        Linking CXX executable cmTC_f92d4.exe
        D:\\Tools\\CMake\\bin\\cmake.exe -E cmake_link_script CMakeFiles\\cmTC_f92d4.dir\\link.txt --verbose=1
        D:\\Tools\\CMake\\bin\\cmake.exe -E rm -f CMakeFiles\\cmTC_f92d4.dir/objects.a
        ar.exe qc CMakeFiles\\cmTC_f92d4.dir/objects.a @CMakeFiles\\cmTC_f92d4.dir\\objects1.rsp
        D:\\Tools\\clang\\bin\\clang.exe -m64  -Wall  -Wextra  -Wshadow  -Wfloat-equal  -Wold-style-cast  -Wconversion  -Werror  -Wsuggest-override  -pedantic  -pedantic-errors  -Wshorten-64-to-32  -fstrict-aliasing  -Wno-deprecated-declarations  -Wno-deprecated  -Wstrict-aliasing  -Wthread-safety  -m64  -Wl,--whole-archive CMakeFiles\\cmTC_f92d4.dir/objects.a -Wl,--no-whole-archive -o cmTC_f92d4.exe -Wl,--out-implib,libcmTC_f92d4.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\\cmTC_f92d4.dir\\linkLibs.rsp
        ld.lld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::basic_string(char const*, std::allocator<char> const&)
        >>> referenced by objects.a(std_regex.cpp.obj):(main)
        ......
        ld.lld: error: undefined symbol: std::runtime_error::runtime_error(char const*)
        >>> referenced by objects.a(std_regex.cpp.obj):(std::regex_error::regex_error(std::regex_constants::error_type, char const*))

        ld.lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
        clang: error: linker command failed with exit code 1 (use -v to see invocation)
        mingw32-make[1]: *** [CMakeFiles\\cmTC_f92d4.dir\\build.make:101: cmTC_f92d4.exe] Error 1
        mingw32-make[1]: Leaving directory 'C:/Users/2283/AppData/Local/.xmake/cache/packages/2408/b/benchmark/1.8.5/source/build_9ec7839e/CMakeFiles/CMakeTmp'
        mingw32-make: *** [Makefile:126: cmTC_f92d4/fast] Error 2

      exitCode: 2
waruqi commented 2 weeks ago

要使用gcc工具链编译Windows上的包需要使用mingw平台(一些包不支持cross平台),但gcc工具链可以独立安装,此时系统中不存在mingw相关环境,包括mingw32-make.exe。而xmake在mingw平台上默认需要该工具

既然不存在 mingw 环境,就不要设置 -p mingw, 那个是专门给 mingw 平台构建用的。

xmake内建的clang和gcc工具链的c++工具链定义中,clang和gcc的定义先于clang++和g++,这会导致在mingw平台上使用上述工具链时,在链接阶段错误的使用clang和gcc去链接c++文件,导致缺少c++标准库进而链接失败。

-p cross 交叉编译就不会存在这个问题。

https://github.com/xmake-io/xmake/blob/8dd37a697c4cb22223908adab6937f464f5caeda/xmake/modules/package/tools/cmake.lua#L595

star-hengxing commented 2 weeks ago

设置成 cross,但有些包限制了。。

24bit-xjkp commented 2 weeks ago

有挺多包描述不支持cross平台的,或者改成包默认支持cross平台?

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


Title: cmake package compilation problem on MinGW platform

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


To use the gcc tool chain to compile packages on Windows, you need to use the mingw platform (some packages do not support the cross platform), but the gcc tool chain can be installed independently. At this time, there is no mingw related environment in the system, including mingw32-make.exe. xmake requires this tool by default on the mingw platform

Since there is no mingw environment, do not set -p mingw, which is specially used for building the mingw platform.

In the c++ toolchain definition of the built-in clang and gcc toolchains in To link the c++ file, the c++ standard library is missing and the link fails.

Cross-compiling with -p cross will not have this problem.

https://github.com/xmake-io/xmake/blob/8dd37a697c4cb22223908adab6937f464f5caeda/xmake/modules/package/tools/cmake.lua#L595

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


Set to cross, but some packages have restrictions. .

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


There are quite a few package descriptions that do not support the cross platform. Or should the package be changed to support the cross platform by default?

waruqi commented 2 weeks ago

试下这个 patch. https://github.com/xmake-io/xmake/pull/5521

24bit-xjkp commented 2 weeks ago
local mingw = assert(package:build_getenv("mingw") or package:build_getenv("sdk"), "mingw not found!")

要用ninja了说明没mingw环境了,这里是不是不应该用断言了?可以改为

local mingw = package:build_getenv("mingw") or package:build_getenv("sdk") or ""
waruqi commented 2 weeks ago

再试试

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


try again

24bit-xjkp commented 2 weeks ago

现在可以正常使用了

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


Now it can be used normally