代码之家  ›  专栏  ›  技术社区  ›  Kevin Berridge

Visual Studio的Vim错误格式

  •  34
  • Kevin Berridge  · 技术社区  · 16 年前

    我想将Vim的快速修复功能与Visual Studio的devenv构建过程或msbuild的输出一起使用。

    我创建了一个名为build.bat的批处理文件,它执行devenv构建,如下所示:

    devenv MySln.sln /Build Debug
    

    在vim中,我将:make命令指向了该批处理文件:

    :set makeprg=build.bat
    

    当我现在运行:make时,构建成功执行,但错误不会被解析出来。因此,如果我运行:cl或:cn,我最终会看到devenv/Build的所有输出。我应该只看到错误。

    我在网络上的各个网站上尝试了许多不同的错误格式设置,但都没有正确解析出错误。以下是我尝试过的几个:

    set errorformat=%*\\d>%f(%l)\ :\ %t%[A-z]%#\ %m
    set errorformat=\ %#%f(%l)\ :\ %#%t%[A-z]%#\ %m
    set errorformat=%f(%l,%c):\ error\ %n:\ %f
    

    当然,我尝试过Vim的默认设置。

    以下是build.bat的一些示例输出:

    C:\TFS\KwB Projects\Thingy>devenv Thingy.sln /Build Debug 
    
    Microsoft (R) Visual Studio Version 9.0.30729.1.
    Copyright (C) Microsoft Corp. All rights reserved.
    ------ Build started: Project: Thingy, Configuration: Debug Any CPU ------
    c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.Linq.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationProvider.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\Thingy.exe /resource:obj\Debug\Thingy.g.resources /resource:obj\Debug\Thingy.Properties.Resources.resources /target:winexe App.xaml.cs Controller\FieldFactory.cs Controller\UserInfo.cs Data\ThingGatewaySqlDirect.cs Data\ThingListFetcher.cs Data\UserListFetcher.cs Gui\FieldList.xaml.cs Interfaces\IList.cs Interfaces\IListFetcher.cs Model\ComboBoxField.cs Model\ListValue.cs Model\ThingType.cs Interfaces\IThingGateway.cs Model\Field.cs Model\TextBoxField.cs Model\Thing.cs Gui\MainWindow.xaml.cs Gui\ThingWindow.xaml.cs Interfaces\IField.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs RequiredValidation.cs "C:\TFS\KwB Projects\Thingy\Thingy\obj\Debug\Gui\FieldList.g.cs" "C:\TFS\KwB Projects\Thingy\Thingy\obj\Debug\Gui\MainWindow.g.cs" "C:\TFS\KwB Projects\Thingy\Thingy\obj\Debug\Gui\ThingWindow.g.cs" "C:\TFS\KwB Projects\Thingy\Thingy\obj\Debug\App.g.cs" "C:\TFS\KwB Projects\Thingy\Thingy\obj\Debug\GeneratedInternalTypeHelper.g.cs"
    C:\TFS\KwB Projects\Thingy\Thingy\Controller\FieldFactory.cs(14,19): error CS0246: The type or namespace name 'IFieldNothing' could not be found (are you missing a using directive or an assembly reference?)
    
    Compile complete -- 1 errors, 0 warnings
    ========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
    

    更新: 看起来使用msbuild而不是devenv可能是正确的做法(正如周的评论所说)。

    使用msbuild,makeprg将是:

    :set makeprg=msbuild\ /nologo\ /v:q
    

    示例输出应为:

    Controller\FieldFactory.cs(14,19): error CS0246: The type or namespace name 'IFieldNothing' could not be found (are you missing a using directive or an assembly reference?)
    

    看起来这里的棘手部分可能在于路径是相对于.csproj文件的,而不是相对于.sln文件的,.sln文件是Vim中的当前目录,位于.csproj档案上方的一个目录。

    答案: 我想通了。..

    set errorformat=\ %#%f(%l\\\,%c):\ %m
    

    这将捕获devenv/Build和msbuild的输出。 然而,msbuild有一个问题。默认情况下,它的输出不包括完整路径。要解决此问题,您必须将以下行添加到csproj文件的主PropertyGroup中:

    <GenerateFullPaths>True</GenerateFullPaths>
    
    7 回复  |  直到 16 年前
        1
  •  22
  •   Kevin Berridge    14 年前

    我有一篇博客文章,详细介绍了在Vim中构建C#项目的所有细节,包括错误格式。你可以在这里找到它: http://kevin-berridge.blogspot.com/2008/09/vim-c-compiling.html

    简而言之,您需要以下内容:

    :set errorformat=\ %#%f(%l\\\,%c):\ %m
    :set makeprg=msbuild\ /nologo\ /v:q\ /property:GenerateFullPaths=true
    
        2
  •  9
  •   Simon Buchan    16 年前

    从问题中复制以从“未回答”列表中删除

    set errorformat=\ %#%f(%l\\\,%c):\ %m
    

    这将捕获两者的输出 devenv /Build 以及msbuild。然而,msbuild有一个问题。默认情况下,它的输出不包括完整路径。要解决此问题,您必须将以下行添加到csproj文件的主PropertyGroup中:

    <GenerateFullPaths>True</GenerateFullPaths>
    
        3
  •  5
  •   idbrii    15 年前

    我找到了一个更好的答案:使用 :compiler 使用内置 efm 设置。

    " Microsoft C#
    compiler cs
    " Microsoft Visual C++
    compiler msvc
    " mono
    compiler mcs
    " gcc
    compiler gcc
    

    注意:它还设置了默认值 makeprg 。请参阅$VIMRUNTIME/编译器/

        4
  •  1
  •   Jay Bazuzi Buck Hodges    16 年前

    请尝试运行msbuild而不是devenv。这将为构建的运行方式带来巨大的动力。

    打开Visual Studio命令提示符以设置路径。那就去吧 msbuild MySln.sln /Configuration:Debug .

    看见 msbuild /? 寻求帮助。

        5
  •  1
  •   idbrii    15 年前

    我在Visual Studio中查找编译c++的errorformat时发现了这个问题。上述答案对我不起作用(我也没有使用MSBuild)。

    我是从 this Vim Tip :help errorformat :

    " filename(line) : error|warning|fatal error C0000: message
    set errorformat=\ %#%f(%l)\ :\ %#%t%[A-z]%#\ %[A-Z\ ]%#%n:\ %m
    

    这将给你一个快速修复,看起来像这样:

    stats.cpp|604 error 2039| 'getMedian' : is not a member of 'Stats'
    

    (突出显示错误)来自

    c:\p4\main\stats.cpp(604) : error C2039: 'getMedian' : is not a member of 'Stats'
    
        6
  •  1
  •   Tom Miller    14 年前

    正如Simon Buchan提到的,您可以在项目中使用它来生成输出中的完整路径:

    <GenerateFullPaths>True</GenerateFullPaths>
    

    但你可以通过添加 /property:GenerateFullPaths=true 给你 makeprg 而不是将上述内容添加到项目文件中。

    :set makeprg=msbuild\ /nologo\ /v:q\ /property:GenerateFullPaths=true\
    
        7
  •  0
  •   manifest    15 年前

    这些错误格式在Visual studio 2009 v9.0.21022.8专业版中都不起作用。使用cygwin时,必须从bash调用devenv,这使得设置makeprg有点棘手(螺旋批处理文件)。当devenv拆分为多个进程并继续显示错误消息“1>”或“2>”等时,还必须调整我的错误格式:

    set autowrite
    "2>c:\cygwin\home\user\proj/blah.cpp(1657) : error C2065: 'blah' : undeclared identifier
    
    set errorformat=%.%#>\ %#%f(%l)\ :\ %#%t%[A-z]%#\ %[A-Z\ ]%#%n:\ %m
    let prg="devenv"
    let makepath=$MAKEPATH
    let &makeprg='cmd /c "'.prg.' '.makepath.'"'
    

    My.bashrc使用cygpath设置MAKEPATH环境变量,以转换为DOS兼容路径:

    export MAKEPATH="$(cygpath -d "proj/VC9/some.sln") /build \"Debug\""
    

    如果你有vim 6.x,你可以使用 :cw 这比clist好得多(试着在数百个警告中搜索错误,你就知道我的意思了)。看着vim的调整让我想吐,但我在vim天堂!!!再见视觉工作室!感谢基地调整pydave+1。

    推荐文章