错误在Windows上使用Clang编译C 11程序

error compiling c++11 program with clang on windows

本文关键字:编译 程序 Clang Windows 错误      更新时间:2023-10-16

我已经在Windows上构建了Clang 3.2,并试图构建一个简单的Hello World程序。但是我遇到了很多错误,就像下面看到的那样。

d:Mariusxyz>clang++ -stdlib=libc++ -std=c++11 -Wall xyz.cpp -o xyz.exe
clang++: warning: argument unused during compilation: '-stdlib=libc++'
In file included from xyz.cpp:12:
In file included from ./stdafx.h:18:
In file included from C:Program Files (x86)Microsoft Visual Studio 11.0VCincludealgorithm:6:
In file included from C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexmemory:6:
In file included from C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexmemory0:9:
In file included from C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexutility:8:
In file included from C:Program Files (x86)Microsoft Visual Studio 11.0VCincludeutility:8:
C:Program Files (x86)Microsoft Visual Studio 11.0VCincludetype_traits:1072:
33: error:
  '_Ty' does not refer to a value
            _HAS_TRIVIAL_MOVE_CONSTRUCTOR(_Ty)
                                          ^
...
C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexstddef:540:50: note:
      expanded from macro '_VARIADIC_EXPAND_4'
#define _VARIADIC_EXPAND_4(FUNC, X1, X2, X3, X4) 
                                                 ^
C:Program Files (x86)Microsoft Visual Studio 11.0VCincludexrefwrap:222:22:note:
      expanded from macro '_CLASS_RESULT_OF_PMF_OPT_0X'
                        __thiscall, X2, X3, X4)
                                          ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

我遇到了很多这样的错误,来自VC 2012标题。Windows上的clang 是否有可能在Windows上构建C 11代码?我必须提供其他命令开关吗?

clang在由Visual Studio构建时无法正常工作(据说它是由MingW构建时起作用的,但我无法验证自己)。

构建Clang时,它被配置为使用用于构建它的库。由于您使用Visual Studio构建了Clang,因此Clang将尝试使用Visual Studio标题和库。不幸的是,Clang和Visual Studio的内部与Clang 无法使用VS特定的标头。

直到可以为Windows编译LIBC 之前,Clang(几乎可以肯定)在由Visual Studio编译时将永远无法使用。尝试使用mingw构建它(并撰写博客文章或有关您的工作方式!),看看它是否对您有用。

clang默认值为用于构建它的工具链的包含。但是,与MS一样,扩展也可能是供应商特定的。Clang自己的LIBC 可用性有望大大减轻该问题。手动指定正确包括dir。

还会在构建过程中分开编译和链接阶段。这将导致链接使用正确的二进制液体,而不是vs默认值。