Windows中的Clang ++ 4.0.0编译错误

时间:2017-10-12 20:47:48

标签: c++ windows clang visual-studio-2017 llvm-clang

我在Windows 10(32位操作系统,x86处理器)上使用从源代码编译的clang 4.0.0。我试图将我的cpp代码编译为llvm ir格式为ll的代码。 这是一个无法使用clang编译的示例代码:

#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代码:

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

编译时出现以下错误:

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文件)。我看过许多帖子,他们引入了一面旗帜:&#39; -fms-compatibility-version = 19&#39;随着命令。但是,添加的标志会被标记为未知参数。我正在使用MSVS 2017.也许我使用这个标志错了。但是我现在已经陷入了这一僵局一天。任何帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

通过在环境变量INCLUDE中添加头文件的正确路径来解决此问题。修复路径后,我的大部分错误都得到了解决。这包括编译引用某些标题(如windows.h)的c ++文件时出错的错误。

关于如何执行相同操作的链接:List all environment variables from command line?

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

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

通过将LIBPATH环境变量设置为指向libcmt.lib文件的正确位置。我找到了我的:

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

这些琐碎的问题让我花了不少时间才弄明白。希望这能为同样的人省下同样的麻烦。