编译 C++ 时出错使用 clang 文件

发布于 2024-09-15 12:45:28 字数 10909 浏览 9 评论 0原文

在 Linux 环境上构建 CLang 后,我尝试使用 Visual Studio 在 Windows 上构建它。一切都很顺利,但是当我尝试编译这个经典的 Hello World! 程序时,

#include <iostream>

int main()
{
   std::cout<<"Hello World!";
}

我遇到了很多错误。

C:\Users\SUPER USER\Desktop>clang++ a.cpp
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/e
xception:38:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/eh.h(58) :  error:
      unknown type name 'type_info'
_CRTIMP int __cdecl _is_exception_typeof(_In_ const type_info &_Type, ...
                                                    ^
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:7:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
string:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
memory:7:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/new(51) :  error:
      exception specification in declaration does not match previous declaration

_Ret_bytecap_(_Size) void *__CRTDECL operator new(size_t _Size) ...
                                     ^
note: previous declaration is here
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/new(79) :  error:
      exception specification in declaration does not match previous declaration

_Ret_bytecap_(_Size) void *__CRTDECL operator new[](size_t _Size)
                                     ^
note: previous declaration is here
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:7:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
string:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
memory:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
utility:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/u
tility:9:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/t
ype_traits:7:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/xtr1common(355) :  erro
r:
      rvalue references are only allowed in C++0x
        struct _Remove_reference<_Ty&&>
                                    ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/xtr1common(368) :  erro
r:
      rvalue references are only allowed in C++0x
        struct _Remove_rvalue_reference<_Ty&&>
                                           ^
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:7:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
string:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
memory:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
utility:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/u
tility:9:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(243) :  err
or:
      rvalue references are only allowed in C++0x
        typedef _Ty && type;
                    ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(427) :  err
or:
      rvalue references are only allowed in C++0x
        struct is_rvalue_reference<_Ty&&>
                                      ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(554) :  err
or:
      '_From' does not refer to a value
        struct is_convertible _IS_CONVERTIBLE(_From, _To)
                              ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:554:40: not
e: instantiated from:
        struct is_convertible _IS_CONVERTIBLE(_From, _To)
                                              ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(553) :  not
e:
      declared here
template<class _From, class _To>
               ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(555) :  err
or:
      expected class name
        {       // determine whether _From is convertible to _To
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(720) :  err
or:
      expected expression
        struct has_nothrow_constructor _HAS_NOTHROW_CONSTRUCTOR(_Ty)
                                       ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:42:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_constructor(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(721) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(726) :  err
or:
      expected expression
        struct has_nothrow_copy _HAS_NOTHROW_COPY(_Ty)
                                ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:44:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_copy(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(727) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow copy constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(733) :  err
or:
      expected expression
        struct has_nothrow_default_constructor _HAS_NOTHROW_CONSTRUCTOR(_Ty)
                                               ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:42:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_constructor(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(734) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(739) :  err
or:
      expected expression
        struct has_nothrow_copy_constructor _HAS_NOTHROW_COPY(_Ty)
                                            ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:44:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_copy(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(740) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow copy constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(746) :  err
or:
      expected expression
        struct has_nothrow_assign _HAS_NOTHROW_ASSIGN(_Ty)
                                  ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:46:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_assign(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(747) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow assignment operator
        ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

C:\Users\SUPER USER\Desktop>

但是当我将相同的 Hello World 程序编译为 C 文件时,一切都很顺利。

 #include <stdio.h>

 int main()
 {
     printf("Hello World!");
 }

 C:\Users\SUPER USER\Desktop>clang a.c

 C:\Users\SUPER USER\Desktop>a.out
 Hello World!

这里的实际问题是什么?

After building CLang on the linux environment I tried to build it on Windows using Visual Studio. Everything went right but when I tried to compile this classic Hello World! program

#include <iostream>

int main()
{
   std::cout<<"Hello World!";
}

I got many errors

C:\Users\SUPER USER\Desktop>clang++ a.cpp
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/e
xception:38:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/eh.h(58) :  error:
      unknown type name 'type_info'
_CRTIMP int __cdecl _is_exception_typeof(_In_ const type_info &_Type, ...
                                                    ^
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:7:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
string:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
memory:7:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/new(51) :  error:
      exception specification in declaration does not match previous declaration

_Ret_bytecap_(_Size) void *__CRTDECL operator new(size_t _Size) ...
                                     ^
note: previous declaration is here
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/new(79) :  error:
      exception specification in declaration does not match previous declaration

_Ret_bytecap_(_Size) void *__CRTDECL operator new[](size_t _Size)
                                     ^
note: previous declaration is here
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:7:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
string:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
memory:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
utility:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/u
tility:9:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/t
ype_traits:7:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/xtr1common(355) :  erro
r:
      rvalue references are only allowed in C++0x
        struct _Remove_reference<_Ty&&>
                                    ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/xtr1common(368) :  erro
r:
      rvalue references are only allowed in C++0x
        struct _Remove_rvalue_reference<_Ty&&>
                                           ^
In file included from a.cpp:1:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/o
stream:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/i
os:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locnum:10:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
treambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
iosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
locale:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/s
tdexcept:7:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
string:6:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
memory:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/x
utility:8:
In file included from C:\Program Files\Microsoft Visual Studio 10.0\VC\include/u
tility:9:
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(243) :  err
or:
      rvalue references are only allowed in C++0x
        typedef _Ty && type;
                    ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(427) :  err
or:
      rvalue references are only allowed in C++0x
        struct is_rvalue_reference<_Ty&&>
                                      ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(554) :  err
or:
      '_From' does not refer to a value
        struct is_convertible _IS_CONVERTIBLE(_From, _To)
                              ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:554:40: not
e: instantiated from:
        struct is_convertible _IS_CONVERTIBLE(_From, _To)
                                              ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(553) :  not
e:
      declared here
template<class _From, class _To>
               ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(555) :  err
or:
      expected class name
        {       // determine whether _From is convertible to _To
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(720) :  err
or:
      expected expression
        struct has_nothrow_constructor _HAS_NOTHROW_CONSTRUCTOR(_Ty)
                                       ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:42:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_constructor(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(721) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(726) :  err
or:
      expected expression
        struct has_nothrow_copy _HAS_NOTHROW_COPY(_Ty)
                                ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:44:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_copy(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(727) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow copy constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(733) :  err
or:
      expected expression
        struct has_nothrow_default_constructor _HAS_NOTHROW_CONSTRUCTOR(_Ty)
                                               ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:42:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_constructor(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(734) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(739) :  err
or:
      expected expression
        struct has_nothrow_copy_constructor _HAS_NOTHROW_COPY(_Ty)
                                            ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:44:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_copy(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(740) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow copy constructor
        ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(746) :  err
or:
      expected expression
        struct has_nothrow_assign _HAS_NOTHROW_ASSIGN(_Ty)
                                  ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits:46:36: note
: instantiated from:
        : _Cat_base<is_pod<_Ty>::value || __has_nothrow_assign(_Ty)>
                                          ^
C:\Program Files\Microsoft Visual Studio 10.0\VC\include/type_traits(747) :  err
or:
      expected class name
        {       // determine whether _Ty has a nothrow assignment operator
        ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

C:\Users\SUPER USER\Desktop>

But everthing went fine when I compiled the same Hello World program as a C file.

 #include <stdio.h>

 int main()
 {
     printf("Hello World!");
 }

 C:\Users\SUPER USER\Desktop>clang a.c

 C:\Users\SUPER USER\Desktop>a.out
 Hello World!

What is the actual problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2024-09-22 12:45:28

看来 clang 不高兴包含 VC10 头文件。我想用VC9试试。

It seems clang is not happy including VC10 header files. I'd try it with VC9.

箹锭⒈辈孓 2024-09-22 12:45:28

根据 http://permalink.gmane.org/gmane.comp.compilers .clang.devel/12930,这是STL头本身的循环依赖问题......

According to http://permalink.gmane.org/gmane.comp.compilers.clang.devel/12930, this is a cyclic dependency problem in the STL headers themselves...

魂ガ小子 2024-09-22 12:45:28
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
   cout << "Hello World!\n" ;
   return 0;
}

尝试一下这个,否则可能是编译器问题......

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
   cout << "Hello World!\n" ;
   return 0;
}

try with this one,otherwise it may be the compiler problem...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文