Windows 10 上的 CMake + MinGW64 + Clang++

CMake + MinGW64 + Clang++ on Windows 10

提问人:c.c 提问时间:10/24/2023 更新时间:10/24/2023 访问量:94

问:

我想使用 Clang++ 10.0、CMake 3.10.1、MinGW x86_64 8.1.0 编译应用程序。在Windows 10.I已经在项目文件夹中的build文件夹中尝试过:

cmake ../  -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_COMPILER_TARGET=x86_64-pc-windows-gnu  -DCMAKE_C_COMPILER_TARGET=x86_64-pc-windows-gnu   -DCMAKE_MAKE_PROGRAM=make -DCMAKE_GENERATOR="MinGW Makefiles" -DCMAKE_RC_COMPILER=make

我可以通过cmake进行编译,结果是:

-- The C compiler identification is Clang 10.0.0 with GNU-like command-line
-- The CXX compiler identification is Clang 10.0.0 with GNU-like command-line
-- Check for working C compiler: D:/LLVM/bin/clang.exe
-- Check for working C compiler: D:/LLVM/bin/clang.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D:/LLVM/bin/clang++.exe
-- Check for working CXX compiler: D:/LLVM/bin/clang++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/workspace/CompileTest/build

但是,当我尝试制作时,它是错误的:

Scanning dependencies of target CC_COMPILE_02
[ 14%] Building CXX object CMakeFiles/CC_COMPILE_02.dir/main.cpp.obj
In file included from D:\workspace\CompileTest\main.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\include\iostream:9:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\include\yvals_core.h:847:1: error: static_assert failed
      "Error in C++ Standard Library usage."
_EMIT_STL_ERROR(STL1000, "Unexpected compiler version, expected Clang 16.0.0 or newer.");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\include\yvals_core.h:502:5: note: expanded from macro '_EMIT_STL_ERROR'
    static_assert(false, "Error in C++ Standard Library usage.")
    ^             ~~~~~
1 error generated.
make[2]: *** [CMakeFiles\CC_COMPILE_02.dir\build.make:62: CMakeFiles/CC_COMPILE_02.dir/main.cpp.obj] Error 1
make[1]: *** [CMakeFiles\Makefile2:75: CMakeFiles/CC_COMPILE_02.dir/all] Error 2
make: *** [Makefile:83: all] Error 2

我的CMakeLists.txt在这里:

cmake_minimum_required(VERSION 3.10)
# set(CMAKE_C_COMPILER_TARGET "x86_64-pc-windows-gnu")
# set(CMAKE_CXX_COMPILER_TARGET "x86_64-pc-windows-gnu")
# set(CMAKE_C_COMPILER "D:/LLVM/bin/clang.exe")
# set(CMAKE_CXX_COMPILER "D:/LLVM/bin/clang++.exe")

project(CC_COMPILE_02)
#add_compile_options(-target x86_64-pc-windows-gnu)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-g")
add_executable(CC_COMPILE_02 main.cpp Tokens/LexerToken.cpp Tokens/LexerToken.h Lexer/Lexer.cpp Lexer/Lexer.h Parser/Parser.cpp Parser/Parser.h Tokens/AbstractToken.cpp Tokens/AbstractToken.h Tokens/ParserToken.cpp Tokens/ParserToken.h Exceptions/CompileException.h Symbol/AbstractSymbol.h Symbol/SymbolTable.h Symbol/IntentSymbol.h Symbol/ArraySymbol.h Symbol/FuncSymbol.h Symbol/SymbolTableController.h)

我认为链接器使用 MSVC 的库,但我已经在 cmake 中指定使用 mingw,我不知道哪里出了问题,想修复它。

我已将 mingw 和 llvm 的 bin 文件夹、include 文件夹和 lib 文件夹放入环境变量中。

C++ Windows CMake MINGW CLang ++

评论

1赞 HolyBlackCat 10/24/2023
一个简单的答案是从 MSYS2 重新安装 Clang 和 MinGW,它们的 Clang 默认使用正确的目标。(当然,如何让 CMake 传递正确的标志仍然很有趣。

答:

0赞 c.c 10/24/2023 #1

我只是使用另一种方法来声明cmake参数并最终通过,这是我更改的指令和CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target x86_64-w64-mingw32")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -target x86_64-w64-mingw32 ")

project(CC_COMPILE_02)
#add_compile_options(-target x86_64-pc-windows-gnu)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-g")
add_executable(CC_COMPILE_02 main.cpp Tokens/LexerToken.cpp Tokens/LexerToken.h Lexer/Lexer.cpp Lexer/Lexer.h Parser/Parser.cpp Parser/Parser.h Tokens/AbstractToken.cpp Tokens/AbstractToken.h Tokens/ParserToken.cpp Tokens/ParserToken.h Exceptions/CompileException.h Symbol/AbstractSymbol.h Symbol/SymbolTable.h Symbol/IntentSymbol.h Symbol/ArraySymbol.h Symbol/FuncSymbol.h Symbol/SymbolTableController.h)

这是我的指示:

cmake ../  -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_COMPILER_TARGET=x86_64-pc-windows-gnu  -DCMAKE_C_COMPILER_TARGET=x86_64-pc-windows-gnu   -DCMAKE_MAKE_PROGRAM=make -DCMAKE_GENERATOR="MinGW Makefiles"

但我仍然不知道为什么它通过了。 如果我删除 CMakeLists.txt 中的这两行:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target x86_64-w64-mingw32")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -target x86_64-w64-mingw32 ")

或者我在说明中删除了它:

-DCMAKE_CXX_COMPILER_TARGET=x86_64-pc-windows-gnu  -DCMAKE_C_COMPILER_TARGET=x86_64-pc-windows-gnu 

它仍然无法通过。我以为这两件事之前有同样的效果,结果让我很困惑。我是CMake的新手,我不太清楚在指令中和在CMakeLists.txt文件中定义编译参数之间的区别。在这个过程中,我经常发现在CMakeList.txt中定义编译参数不起作用,只能在命令行上定义。我不知道问题出在哪里。

评论

0赞 Oersted 10/24/2023
不确定是否有帮助,但似乎最好通过命令行设置编译器:discourse.cmake.org/t/...
0赞 HolyBlackCat 10/24/2023
或者工具链文件。您不应将这些详细信息硬编码到 CMakeLists.txt 本身中以保持其可移植性。