如何在Windows上使用clang和mingw-w64头文件

23

我从http://llvm.org/releases/3.9.0/LLVM-3.9.0-win32.exe安装了clang 3.9。

clang version 3.9.0 (branches/release_39)
Target: i686-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

使用gcc 6.2.0 (Mingw-w64)

gcc (i686-posix-dwarf-rev1, Built by MinGW-W64 project) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我电脑上没有安装MSVC和Windows SDK,我需要clang的某些特定功能,并希望将其替换为g++,因为我使用C++。当我尝试编译简单文件时,出现以下错误:

fatal error: 'string' file not found

这是否意味着我的当前clang安装不支持在Windows上使用mingw?基本上,我想做的就是使用来自我的mingw-w64安装的头文件和库。我正在寻找解决方案,但并没有找到任何有用的信息。我不知道该怎么办。

这是否也意味着我的clang安装依赖于我没有的MSVC?

编辑: 从此页面的评论:http://blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt2/

这不再适用于最新的二进制文件(3.7.1、3.8、3.9)了,因为这些文件是使用Visual Studio和为Visual Studio编译的。

您可以安装完整的Visual Studio 2015(大约需要8GB),或者安装“Microsoft Visual C++ Build Tools 2015 Update 3”,其中包含VS的命令行工具和Clang需要的标准C++头文件。

我觉得这解释了一切。这是否意味着我需要使用mingw构建才能使其与mingw配合使用?

2个回答

34

正确的做法是这样的...

clang -target i686-pc-windows-gnu test.c -otest.exe

或者,如果您想要64位输出...

clang -target x86_64-pc-windows-gnu test.c -otest.exe

Clang会根据您的路径确定头文件和库的位置。确保您的路径中只有您正在定位的Mingw版本。

默认情况下,当前版本的Clang(目前为5.0.0)将针对x86_64-pc-windows-msvc进行目标设置。

例如...

Microsoft Windows [Version 10.0.16299.19]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\burito>copy con hello.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello World!\n");
    return 0;
}
^Z
        1 file(s) copied.

C:\Users\burito>clang hello.c -ohello.exe --verbose
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
clang.exe: warning: unable to find a Visual Studio installation; try running Clang from a developer command prompt [-Wmsvc-not-found]
 "C:\\Program Files\\LLVM\\bin\\clang.exe" -cc1 -triple x86_64-pc-windows-    msvc18.0.0 -emit-obj -mrelax-all -mincremental-linker-compatible -disable-free -    disable-llvm-verifier -discard-value-names -main-file-name hello.c -mrelocation-    model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -v -dwarf-column-info -debugger-tuning=gdb -resource-dir "C:\\Program Files\\LLVM\\lib\\clang\\5.0.0" -internal-isystem "C:\\Program Files\\LLVM\\lib\\clang\\5.0.0\\include" -internal-isystem C:/Program Files/Microsoft Visual Studio 10.0/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 9.0/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include -internal-isystem C:/Program Files/Microsoft Visual Studio 8/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include -fdebug-compilation-dir "C:\\Users\\burito" -ferror-limit 19 -fmessage-length 120 -fms-extensions -fms-compatibility -fms-compatibility-version=18 -fno-threadsafe-statics -fdelayed-template-parsing -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o "C:\\Users\\burito\\AppData\\Local\\Temp\\hello-5c526d.o" -x c hello.c
clang -cc1 version 5.0.0 based upon LLVM 5.0.0 default target x86_64-pc-windows-msvc
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 10.0/VC/include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 9.0/VC/include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 8/VC/include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
#include "..." search starts here:
#include <...> search starts here:
 C:\Program Files\LLVM\lib\clang\5.0.0\include
End of search list.
hello.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.

C:\Users\burito>clang hello.c -ohello.exe --verbose -target x86_64-pc-windows-gnu
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-pc-windows-gnu
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 "C:\\Program Files\\LLVM\\bin\\clang.exe" -cc1 -triple x86_64-pc-windows-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.c -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -momit-leaf-frame-pointer -v -dwarf-column-info -debugger-tuning=gdb -resource-dir "C:\\Program Files\\LLVM\\lib\\clang\\5.0.0" -internal-isystem "C:\\Program Files\\LLVM\\lib\\clang\\5.0.0\\include" -internal-isystem "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\x86_64-w64-mingw32/sys-root/mingw/include" -internal-isystem "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\x86_64-w64-mingw32\\include" -internal-isystem "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\include" -fdebug-compilation-dir "C:\\Users\\burito" -ferror-limit 19 -fmessage-length 120 -fno-use-cxa-atexit -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o "C:\\Users\\burito\\AppData\\Local\\Temp\\hello-d88ba4.o" -x c hello.c
clang -cc1 version 5.0.0 based upon LLVM 5.0.0 default target x86_64-pc-windows-msvc
ignoring nonexistent directory "C:\Program Files\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev0\mingw64\x86_64-w64-mingw32/sys-root/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 C:\Program Files\LLVM\lib\clang\5.0.0\include
 C:\Program Files\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev0\mingw64\x86_64-w64-mingw32\include
 C:\Program Files\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev0\mingw64\include
End of search list.
 "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\bin\\ld.exe" -m i386pep -Bdynamic -o hello.exe "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\x86_64-w64-mingw32\\lib\\crt2.o" "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\7.2.0\\crtbegin.o" "-LC:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\7.2.0" "-LC:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\x86_64-w64-mingw32\\lib" "-LC:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\lib" "-LC:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\x86_64-w64-mingw32/sys-root/mingw/lib" "C:\\Users\\burito\\AppData\\Local\\Temp\\hello-d88ba4.o" -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\7.2.0\\crtend.o"

C:\Users\burito>hello
Hello World!

C:\Users\burito>

1
问题是如何在mingw-w64头文件中使用它,而不是如何构建不同架构的问题。这对某些人来说并不清楚。例如,当我开始时,我的电脑上没有msvc,但我尝试在结尾处构建 -msvc。正如您所说,最新版本默认针对msvc,那时就是我的问题所在。没有简单的方法只选择使用gnu而不是他们的最新版本,因此我有三种方法-降级,使用MSYS2或自己构建。 - Konrad
5
以下是翻译的结果:这就是如何使用MinGW-w64头文件。如果已经安装了mingw并且在您的路径中,那么target参数就是使clang使用头文件的方法,只需确保选择与您安装的mingw版本相对应的位版本。 - burito
我刚测试了一下,Clang 3.9.0 也有这个行为。 - burito
有趣,那么你的答案是正确且非常简单的! - Konrad
2
请使用clang 6.0.0和MinGW-w64最新版本(gcc 7.30)进行测试。以上代码可正常工作。 - jdhao
显示剩余4条评论

6

安装旧版本的MinGW,即3.7.0 RC3,它能够工作。

你可以注意到版本差异在于i686-pc-windows-gnu vs i686-pc-windows-msvc

我认为如果他们没有在源代码中弄坏什么东西并且您自己构建了它,那么clang 3.9也可以工作。但是最好能够让最新的3.9与MinGW一起工作。我懒得尝试自己构建它。

编辑: 也有一些适用于MinGW的软件包https://github.com/Alexpux/MINGW-packages,您可以使用MSYS2进行安装,这些软件包似乎提供了对clang的支持。有一些与3.8和3.9相关的提交。

希望这将有助于遇到类似问题的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接