
【cpp/c++ summary 工具】Visual Studio 2022 切换编译器(Clang、MinGW Minimalist GNU for Windows)
_bf16。
·
- 编译项目时出现了:Compiler ‘MSVC’ is not supported 的CMake Error
- 某些项目可能不支持 MSVC,或专为 GCC、Clang 等编译器设计(比如项目中用到了
__attribute__
,且并非所有的编译器都支持 bfloat16 类型__bf16
) - 报错的CMAKE语句如下:
#
# Copyright Soramitsu Co., 2021-2023
# Copyright Quadrivium Co., 2023
# All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#
if (CMAKE_CXX_COMPILER_ID MATCHES "^(AppleClang|Clang)$")
print("Using Clang")
include(${CMAKE_CURRENT_LIST_DIR}/compiler/clang.cmake)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
print("Using GCC")
include(${CMAKE_CURRENT_LIST_DIR}/compiler/gcc.cmake)
else()
fatal_error("Compiler '${CMAKE_CXX_COMPILER_ID}' is not supported")
endif()
- Clang、MinGW 和 MSVC 是三种常用的编译器或编译工具链,它们在支持的平台、编译目标和功能上各有不同。下面是它们之间的主要区别:
特性 | Clang | MinGW | MSVC |
---|---|---|---|
平台 | macOS、Linux、Windows | Windows | Windows |
编译标准 | 支持最新的 C 和 C++ 标准 | 基于 GCC,支持 C/C++ | 支持最新的 C/C++ 标准,但相对较慢 |
工具链 | 基于 LLVM,可与 MSVC 集成 | GNU 工具链(基于 GCC/GNU Compiler Collection) | 完全微软生态(专有工具链) |
库支持 | 依赖 LLVM 库,跨平台支持更好 | 依赖开源库,通常无需微软库 | 依赖微软运行时库 |
典型应用 | 跨平台开发,尤其是 macOS 和 Linux | 在 Windows 上使用 GNU 工具链 | Windows 原生开发,Visual Studio 环境 |
- 从CMake 代码片段可以看出,该项目当前仅支持 Clang 和 GCC 编译器,下面的部分进行了 Visual Studio 2022 切换编译器。
- 需要改变CMake的工具集配置:
vs2022 Clang
- Clang安装较为简单。点击“修改”,并勾选安装C++ Clang开发工具(在vs2022的安装工具的可选部分添加Clang编译器支持)
- 切换Cmake设置:
vs2022 MinGW
- 但是再次编译时,会产生如下错误:
1> INCLUDE=${env.INCLUDE};C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32;C:/msys64/mingw64/include/c++/9.1.0;C:/msys64/mingw64/include/c++/9.1.0/tr1;C:/msys64/mingw64/include/c++/9.1.0/x86_64-w64-mingw32
1> 命令行: "C:\windows\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\multisim\Desktop\soralog\soralog-master\out\install\Mingw64-Debug" -DCMAKE_C_COMPILER:STRING="C:/msys64/mingw64/bin/gcc.exe" -DCMAKE_CXX_COMPILER:STRING="C:/msys64/mingw64/bin/g++.exe" -DCMAKE_MAKE_PROGRAM="C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\Users\multisim\Desktop\soralog\soralog-master" 2>&1"
1> 工作目录: C:\Users\multisim\Desktop\soralog\soralog-master\out\build\Mingw64-Debug
1> [CMake] Binary cache uploading is DISABLED.
1> [CMake] -- The CXX compiler identification is unknown
1> [CMake] CMake Error at C:\Users\multisim\Desktop\soralog\soralog-master\CMakeLists.txt:13 (project):
1> [CMake] The CMAKE_CXX_COMPILER:
1> [CMake]
1> [CMake] C:/msys64/mingw64/bin/g++.exe
1> [CMake]
1> [CMake] is not a full path to an existing compiler tool.
1> [CMake]
1> [CMake] Tell CMake where to find the compiler by setting either the environment
1> [CMake] variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
1> [CMake] to the compiler, or to the compiler name if it is in the PATH.
1> [CMake]
1> [CMake]
1> [CMake] -- Configuring incomplete, errors occurred!
1> “C:\windows\system32\cmd.exe”“/c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\multisim\Desktop\soralog\soralog-master\out\install\Mingw64-Debug" -DCMAKE_C_COMPILER:STRING="C:/msys64/mingw64/bin/gcc.exe" -DCMAKE_CXX_COMPILER:STRING="C:/msys64/mingw64/bin/g++.exe" -DCMAKE_MAKE_PROGRAM="C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\Users\multisim\Desktop\soralog\soralog-master" 2>&1"”执行失败,出现错误:““C:\windows\system32\cmd.exe”“/c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\multisim\Desktop\soralog\soralog-master\out\install\Mingw64-Debug" -DCMAKE_C_COMPILER:STRING="C:/msys64/mingw64/bin/gcc.exe" -DCMAKE_CXX_COMPILER:STRING="C:/msys64/mingw64/bin/g++.exe" -DCMAKE_MAKE_PROGRAM="C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe" -DCMAKE_TOOLCHAIN_FILE="C:/Program Files/Microsoft Visual Studio/2022/Community/VC/vcpkg/scripts/buildsystems/vcpkg.cmake" "C:\Users\multisim\Desktop\soralog\soralog-master" 2>&1"”已返回,并带有退出代码: 1”。
- 下载 mingw-w64:https://github.com/niXman/mingw-builds-binaries/releases
- 将编译器地址添加到如下的CMake变量中:
- 添加gcc的路径到环境变量中,并进行如下修改:
{
"name": "CMAKE_C_COMPILER",
"value": "gcc.exe",
"type": "STRING"
},
{
"name": "CMAKE_CXX_COMPILER",
"value": "g++.exe",
"type": "STRING"
}
- 也可修改以下的变量:MINGW64_ROOT和BIN_ROOT
重新配置CMake缓存
- 效果如下:
更多推荐
所有评论(0)