使用控制台和MSBuild的Cpp核心指南检查器

时间:2018-04-10 15:48:52

标签: c++ visual-studio msbuild visual-studio-2017 cpp-core-guidelines

我有一些设置用于构建的项目的解决方案。 我想运行指南检查器来寻求解决方案。使用这个MSDN article我不想修改项目文件,所以我创建了batfile:

call "%VS140COMNTOOLS%\VsMSBuildCmd.bat"
msbuild Olymp.sln /p:EnableCppCoreCheck=true /p:RunCodeAnalysis=true /p:CodeAnalysisRuleSet="C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckRules.ruleset" /t:Rebuild

项目设置为不执行代码分析 enter image description here

但我使用bat文件覆盖它 当我运行此代码时,我没有关于警告的输出(如在MSDN上),它存在:

E:\Code\Olymp\Olymp>msbuild Olymp.sln /p:EnableCppCoreCheck=true /p:RunCodeAnalysis=true /p:CodeAnalysisRuleSet="C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckRules.ruleset" /t:Rebuild
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
Build started 10.04.2018 18:42:59.
Project "E:\Code\Olymp\Olymp\Olymp.sln" on node 1 (Rebuild target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|x64".
Project "E:\Code\Olymp\Olymp\Olymp.sln" (1) is building "E:\Code\Olymp\Olymp\Olymp\Olymp.vcxproj" (2) on node 1 (Rebuil
d target(s)).
_PrepareForClean:
  Deleting file "x64\Debug\Olymp.tlog\Olymp.lastbuildstate".
InitializeBuildStatus:
  Creating "x64\Debug\Olymp.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /D CODE_
  ANALYSIS /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"x64\Debug\\" /Fd"x64\D
  ebug\vc140.pdb" /Gd /TP /analyze /errorReport:queue   /analyze:quiet main.cpp
  main.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\link.exe /ERRORREPORT:QUEUE /OUT:"E:\Code\Olymp\
  Olymp\x64\Debug\Olymp.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.
  lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiA
  ccess='false'" /manifest:embed /DEBUG:FASTLINK /PDB:"E:\Code\Olymp\Olymp\x64\Debug\Olymp.pdb" /TLBID:1 /DYNAMICBASE /
  NXCOMPAT /IMPLIB:"E:\Code\Olymp\Olymp\x64\Debug\Olymp.lib" /MACHINE:X64 x64\Debug\main.obj
  Olymp.vcxproj -> E:\Code\Olymp\Olymp\x64\Debug\Olymp.exe
  Olymp.vcxproj -> E:\Code\Olymp\Olymp\x64\Debug\Olymp.pdb (Partial PDB)
FinalizeBuildStatus:
  Deleting file "x64\Debug\Olymp.tlog\unsuccessfulbuild".
  Touching "x64\Debug\Olymp.tlog\Olymp.lastbuildstate".
Done Building Project "E:\Code\Olymp\Olymp\Olymp\Olymp.vcxproj" (Rebuild target(s)).

Done Building Project "E:\Code\Olymp\Olymp\Olymp.sln" (Rebuild target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.68

是的,我可以从项目中收集所有cpps并尝试将Analyzer作为cl.exe参数运行,但我想使用msbuild。 我的设置有什么问题?

1 个答案:

答案 0 :(得分:1)

  

使用控制台和MSBuild的Cpp核心指南检查器

我发现您已通过脚本call "%VS140COMNTOOLS%\VsMSBuildCmd.bat"为Visual Studio 2015命令行设置了环境变量。 %VS140COMNTOOLS%用于为Visual Studio 2015命令行而不是Visual Studio 2017设置环境变量。然后,您可以成功构建项目而不会出现任何错误,因此我怀疑此项目可能由Visual Studio 2015创建。

如果是,您将获得结果而没有关于警告的输出。这是因为默认情况下在Visual Studio 2017中安装了核心指南检查器,而不是在Visual Studio 2015中they are available as a NuGet package for Visual Studio 2015

要为Visual Studio 2017命令行设置环境变量,您可以从以下位置调用VsMSBuildCmd.bat

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools

由于您使用的是预览版Visual Studio,因此应该是:

C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\Common7\Tools

要通过MSBuild进行验证,我使用Visual Studio 2017创建了一个测试示例项目(不修改项目文件),然后创建了一个包含以下脚本的bat文件:

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsMSBuildCmd.bat"
MSBuild "C:\Users\Admin\Source\repos\ConsoleApplication1\ConsoleApplication1.sln" /p:EnableCppCoreCheck=true /p:RunCodeAnalysis=true /p:CodeAnalysisRuleSet="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckRules.ruleset" /t:Rebuild
pause

执行bat文件后,我收到了警告:

enter image description here

要验证这一点,您还可以使用Visual Studio 2017创建一个新项目,然后使用该bat文件构建它。

希望这有帮助。