开始使用 clang 3.6.0 和 Visual Studio 2013;如何解决"unknown argument: -ftemplate-depth"编译器错误?

Started using clang 3.6.0 with Visual Studio 2013; how do I solve my "unknown argument: -ftemplate-depth" compiler error?

本文关键字:解决 unknown argument 错误 编译器 -ftemplate-depth 何解决 clang 2013 Studio Visual      更新时间:2023-10-16

我以前从未使用过clang,我认为现在是熟悉它的最佳时机。我安装了最新的Windows快照构建,它与Visual Studio集成在一起。

为了测试它,我开始做Project Euler问题。这是我用来解决问题1的代码:

#include <iostream>
template <unsigned int m, unsigned int n>
struct Mult
{
  enum { Value = 0 };
};
template <>
struct Mult < 0, 0 >
{
  enum { Value = 1 };
};
template <unsigned int m>
struct Mult < m, 0 >
{
  enum { Value = 1 }; 
};
template <unsigned int n>
struct Mult < 0, n >
{
  enum { Value = 1 };
};
template < unsigned int n >
struct EulerOne
{
  enum { Value = (n * Mult<n % 3, n % 5>::Value) + EulerOne<n-1>::Value };
};
template <>
struct EulerOne < 0 >
{
  enum { Value = 0 };
};
using namespace std;
int main(void)
{
  cout << "Sum: " << EulerOne<999>::Value << endl;
  return 0;
}

不一定是最好的代码,但仍然有效。我遇到的问题是模板初始化。微软的C++编译器被锁定在500的递归限制下,无法更改。clang应该有-fmplate depth=NNN选项。因此,我将-ftemplate-depth=1024添加到项目的"附加选项"设置中。

我在编译时遇到的错误不是代码错误,而是编译器特定的错误:

1>clang-cl.exe : error : unknown argument: '-ftemplate-depth=1024'

我检查了一下,以确保我没有下载一些奇怪的、实验性的构建。我甚至做了以下事情:

> type "C:Program Files (x86)LLVMbinclang-cl.exe" | find "template-depth"
use -ftemplate-depth=N to increase recursive template instantiation depth
ftemplate-depth-
ftemplate-depth=
ftemplate-depth
-ftemplate-depth

我尝试使用-Xclang "-ftemplate-depth=1024",但最终出现了1>CL : error : unknown argument: '-ftemplate-depth=1024'编译错误。

添加-v选项后,这是它告诉我要运行的命令:

"C:\Program Files (x86)\LLVM\msbuild-bin\CL.exe" -cc1 -triple i686-pc-windows-msvc -emit-obj -disable-free -main-file-name p1.cpp -mrelocation-model static -mdisable-fp-elim -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -target-cpu pentium4 -D_MT -D_DLL --dependent-lib=msvcrt --dependent-lib=oldnames -fexceptions -fcxx-exceptions -fdiagnostics-format msvc -v -gline-tables-only -dwarf-column-info -coverage-file "C:\Users\jsmith\documents\visual studio 2013\Projects\ProjectEulerCpp\P1\LLVM\p1.obj" -resource-dir "C:\Program Files (x86)\LLVM\msbuild-bin\..\lib\clang\3.6.0" -internal-isystem "C:\Program Files (x86)\LLVM\msbuild-bin\..\lib\clang\3.6.0\include" -internal-isystem "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" -internal-isystem "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include" -internal-isystem "C:\Program Files (x86)\Windows Kits\8.1\Include\um" -internal-isystem "C:\Program Files (x86)\Windows Kits\8.1\Include\shared" -internal-isystem "C:\Program Files (x86)\Windows Kits\8.1\Include\winrt" -O2 -Wall -Wno-error -std=c++11 -fdeprecated-macro -fdebug-compilation-dir "C:\Users\jsmith\documents\visual studio 2013\Projects\ProjectEulerCpp\P1" -ferror-limit 19 -fmessage-length 0 -mstackrealign -fms-extensions -fms-compatibility -fms-compatibility-version=18.0 -fdelayed-template-parsing -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -ftemplate-depth=1024 -o "LLVM\p1.obj" -x c++ p1.cpp

无论我如何分割,我都无法使用此选项。有人有什么建议吗?

似乎是

clang-cl -Xclang -ftemplate-depth -Xclang 1024 test.cpp

我是怎么得到的:

gcc风格的驱动程序clang++可以很好地接受-ftemplate-depth=1024,因此我们可以查看它在指定该选项时生成的命令行:

C:LLVMbin>clang++ -v -ftemplate-depth=1024 -c test.cpp
clang version 3.5.0 (217039)
Target: i686-pc-windows-gnu
Thread model: posix
 "C:LLVMbinclang++.exe" -cc1 -triple i686-pc-windows-gnu -emit-obj -mrelax-al
l -disable-free -main-file-name test.cpp -mrelocation-model static -mdisable-fp-
elim -fmath-errno -masm-verbose -mconstructor-aliases -target-cpu pentium4 -v -d
warf-column-info -coverage-file "C:\LLVM\bin\test.o" -resource-dir "C:\LLVM
bin\..\lib\clang\3.5.0" -fdeprecated-macro -fdebug-compilation-dir "C:\LLV
M\bin" -ftemplate-depth 1024 -ferror-limit 19 -fmessage-length 80 -mstackrealig
n -fno-use-cxa-atexit -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnos
tics-show-option -fcolor-diagnostics -o test.o -x c++ test.cpp

关键是,该选项作为两个命令行参数传递,-ftemplate-depth后跟1024。因此将其应用于CCD_ 10得到CCD_。