关于c ++:Windows中的Clang ++ 4.0.0编译错误

Clang++ 4.0.0 compilation error in windows

我正在使用从Windows10(32位操作系统,x86处理器)源代码编译的clang 4.0.0。我正在尝试将cpp代码编译为ll格式的llvm ir代码。下面是一个未能使用clang编译的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <windows.h>
#include <tlhelp32.h>
#include <fstream>
#include <mapi.h>
#include <memory>
#include<stdio.h>

/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}

/* Program to test function power */
int main()
{
    int x = 2;
    unsigned int y = 3;

    printf("%d", power(x, y));
    return 0;
}

这只是一个示例代码,但它在所有C++代码中都是相同的行为,其中包括列出的头文件。我使用以下命令将其编译为llvm ir代码:

1
clang++ -S -emit-llvm assiral.cpp -o assiral.ll

编译时,我得到以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
C:\Sample>clang++ -emit-llvm -S assiral.cpp -o assiral.ll
In file included from assiral.cpp:3:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:7:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\cmath:617:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xtgmath.h:9:
C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xtr1common:204:22: error:
use of undeclared identifier 'char16_t'
struct _Is_integral<char16_t>
^
C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xtr1common:210:22: error:
use of undeclared identifier 'char32_t'
struct _Is_integral<char32_t>
^
In file included from assiral.cpp:3:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:10:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\streambuf:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xiosbase:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocale:8:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\stdexcept:6:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\exception:7:
In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\type_traits:6:
C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstddef:257:2: error: 'auto'
  return without trailing return type; deduced return types are a C++14 extension
auto _Unfancy(_Ptrty _Ptr)
  ^
  In file included from assiral.cpp:3:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:10:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\streambuf:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xiosbase:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocale:8:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\stdexcept:7:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:8:
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\limits:612:33: error: use of
  undeclared identifier 'char16_t'
  template<> class numeric_limits<char16_t>
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\limits:902:33: error: use of
  undeclared identifier 'char32_t'
  template<> class numeric_limits<char32_t>
  ^
  In file included from assiral.cpp:3:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:10:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\streambuf:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xiosbase:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocale:8:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\stdexcept:7:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:10:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xutility:8:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\utility:7:
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\iosfwd:254:21: error: use of
  undeclared identifier 'char16_t'
  struct char_traits<char16_t>
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\iosfwd:263:21: error: use of
  undeclared identifier 'char32_t'
  struct char_traits<char32_t>
  ^
  In file included from assiral.cpp:3:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:10:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\streambuf:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xiosbase:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocale:8:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\stdexcept:7:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:10:
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xutility:698:2: error:
  'auto' return without trailing return type; deduced return types are a C++14 extension
auto _Unchecked_n(_Iter _Src, _Diff)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xutility:742:2: error:
  'auto' return without trailing return type; deduced return types are a C++14 extension
auto _Unchecked_n_backward(_Iter _Src, _Diff)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xutility:1142:12: error:
  deduced return types are a C++14 extension
constexpr decltype(auto) _Operator_arrow(_Iterator&& _Target, false_type)
  ^
  In file included from assiral.cpp:3:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:10:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\streambuf:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xiosbase:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocale:8:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\stdexcept:7:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:6:
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:356:2: error:
  'auto' return without trailing return type; deduced return types are a C++14 extension
auto _Const_cast(_Ptrty _Ptr)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:366:2: error:
  'auto' return without trailing return type; deduced return types are a C++14 extension
auto _Const_cast(_Ty * _Ptr)
  ^
  In file included from assiral.cpp:3:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\fstream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\includem:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ostream:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\ios:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocnum:10:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\streambuf:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xiosbase:6:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xlocale:8:
  In file included from C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\stdexcept:7:
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:75:2: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Start_at; ; ++_Match_try)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:119:3: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Min_value(_Start_at, _Hay_size - _Needle_size); ; --_Match_try)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:144:3: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Min_value(_Start_at, _Hay_size - 1); ; --_Match_try)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:169:3: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:188:3: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Min_value(_Start_at, _Hay_size - 1); ; --_Match_try)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:213:3: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try)
  ^
  C:\Program Files\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xstring:233:3: error:
  statement not allowed in constexpr function
  for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try)
  ^
  fatal error: too many errors emitted, stopping now [-ferror-limit=]
  20 errors generated.

运行该命令后,我期望生成字节码文件(.ll文件)。我看过很多文章,他们在其中引入了一个标志:"-fms compatibility version=19"以及命令。但是,添加的标志被标记为未知参数。我正在与2017年的MSV合作。也许我用这个旗子弄错了。但我已经陷入僵局一天了。任何帮助或建议都将不胜感激。


这是通过向环境变量include中的头文件添加正确的路径来修复的。一旦我修正了路径,我的大部分错误就解决了。这包括编译C++文件时出现的错误,这些文件引用了某些窗口,如Windows .h给出了未找到的文件。

如何做相同的链接:从命令行列出所有环境变量?

您还可以解决另一个问题,即:

1
link fatal error lnk1104 cannot open file 'libcmt.lib'

通过将libpath环境变量设置为指向libcmt.lib文件的正确位置。我的发现地点:

1
C:\Program Files\Microsoft Visual Studio 14.0\VC\lib

这些琐碎的问题让我花了好几天的时间来解决。希望这能给别人省去同样的麻烦。