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

5

我已经在Windows上构建了clang 3.2,并尝试构建一个简单的hello world程序。但是我遇到了很多错误,就像下面看到的那个错误。

d:\Marius\xyz>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.0\VC\include\algorithm:6:
In file included from C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory:6:
In file included from C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0:9:
In file included from C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xutility:8:
In file included from C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\utility:8:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\type_traits:1072:
33: error:
  '_Ty' does not refer to a value
            _HAS_TRIVIAL_MOVE_CONSTRUCTOR(_Ty)
                                          ^

...

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xstddef: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.0\VC\include\xrefwrap: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++ 构建 C++11 代码是否有可能?我需要提供额外的命令开关吗?


可能是 -fms-extensions - arrowd
根据用户手册(http://clang.llvm.org/docs/UsersManual.html),该标志默认已启用。我还尝试设置-fmsc-version=1700(即VS2012),甚至是-fdelayed-template-parsing(这也应该是默认值)。但没有结果。删除stdafx.h对编译错误没有影响。 - Marius Bancila
我也尝试使用带有-isystem的mingw头文件,但这似乎是一个配置噩梦。根据包含的顺序,我会得到不同的错误,并且无法设置一个没有编译错误的顺序。 - Marius Bancila
@Mat,stdafx.h文件与Microsoft无关。它是一个项目特定的文件,完全符合标准。如果您打开某些选项,Visual Studio编译器只会缓存已编译的输出。 - moswald
2个回答

3

在使用 Visual Studio 编译 Clang 时会出现问题(据说使用 MinGW 编译可以正常工作,但我自己还没有验证过)。

构建 Clang 时,配置文件会指定使用构建它的库文件。由于你是使用 Visual Studio 构建 Clang,因此 Clang 将尝试使用 Visual Studio 的头文件和库文件。不幸的是,Clang 和 Visual Studio 的内部差异足够大,以至于 Clang 无法 使用 VS 特定的头文件。

在 Windows 平台上,只有在能够编译出 libc++ 后,Clang 才可能正常工作,而这几乎肯定需要使用 MinGW 进行构建(请写一篇博客之类的文章,介绍一下你是如何完成这个过程的!),建议你尝试这样做,看看是否成功。


0

clang默认使用构建它的工具链的包含文件。但是在MS的情况下,使用的扩展可能是供应商特定的。预计clang自己的libc++可用性将极大地缓解这个问题。手动指定正确的包含目录。

还要在构建过程中分离编译和链接阶段。这将导致链接使用正确的二进制库而不是VS默认值。


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