在Windows环境下使用clang出现编译错误

A compilation error occurs when using clang in a Windows environment

我用 Vscode 编译了代码。 clang -v:

clang version 14.0.3
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/mingw64/bin

你可以看到我从 msys 中获取了 clang。

我编译的文件使用了这样的头文件:

#include <windows.h>
#include <windowsx.h>
#include <tchar.h>

#include <d2d1.h>

我使用的任务:

"args": [
            "-fdiagnostics-color=always",
            "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x86\user32.lib",
            "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x86\ole32.lib",
            "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x86\d2d1.lib",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\${fileBasenameNoExtension}.exe",
            "-mwindows"
            ],

编译器消息:

C:\msys64\mingw64\bin\clang-cpp.exe -fdiagnostics-color=always "C:\Program Files (x86)\Windows Kits\Lib.0.22000.0\um\x86\user32.lib" "C:\Program Files (x86)\Windows Kits\Lib.0.22000.0\um\x86\ole32.lib" "C:\Program Files (x86)\Windows Kits\Lib.0.22000.0\um\x86\d2d1.lib" -g C:\Users\cookie\Desktop\GameEngineFromScratch\Platform\Windows\helloengine_d2d.cpp -o C:\Users\cookie\Desktop\GameEngineFromScratch\Platform\Windows\helloengine_d2d.exe -mwindows
clang-cpp: warning: C:\Program Files (x86)\Windows Kits\Lib.0.22000.0\um\x86\user32.lib: 'linker' input unused in cpp mode [-Wunused-command-line-argument]
clang-cpp: warning: C:\Program Files (x86)\Windows Kits\Lib.0.22000.0\um\x86\ole32.lib: 'linker' input unused in cpp mode [-Wunused-command-line-argument]
clang-cpp: warning: C:\Program Files (x86)\Windows Kits\Lib.0.22000.0\um\x86\d2d1.lib: 'linker' input unused in cpp mode [-Wunused-command-line-argument]
clang-cpp: warning: argument unused during compilation: '-mwindows' [-Wunused-command-line-argument]

我得到了一个可执行文件,但是当我 运行 它时,我的终端给我一条错误消息 Unable to run: The specified executable is not a valid application for this operating system platform

看起来我得到了 linux 的可执行文件,但没有 windows.How 来解决这个问题。

clang-cpp 是 Clang 预处理器 ,不是 C++ 编译器。您应该为 C++ 编译器 front-end 程序使用 clang++