简体   繁体   中英

Attach VS2010 Debugger to GTest running from MSBuild

I Compile my GTests using my own configuration:

<ProjectConfiguration Include="Test|Win32">
  <Configuration>Debug</Configuration>
  <Platform>Win32</Platform>
</ProjectConfiguration>

I Run my GTests using a custom made Target

  <Target Name="RunTests">
    <Message Text="Configuration: $(Configuration)" Importance="High" />
    <Exec Command="..\$(Configuration)\@(BuildProjectFile->'%(Filename)').exe" />
  </Target>

I would like to run my GTest using VS2010 instead of running it as its own executeable. I want the advantage of using the VS2010 debugger to debug my tests.

Is there a correct task to start my compilation so the debugger picks it up or do i rather have to return something to VS2010 so it starts executing on its own?

By using Returns="@(ManagedTargetPath)" VS is getting the executeable is is supposed to start.

Instead of starting the Test via the Exec Task the Target should just be Build with Debugging enables and the TargetPath should be returned to VS for execution.

The following example redirects an incoming build call to execute the unittests instead and launch them using the build in mechanism of VS2010.

(I currently do not understand why breakpoints in the *.cc files are ignored and the ones in the *.c files are not ignored - could be a coincidence)

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <ItemGroup>
    <ClCompile Include="main.c" />
    <ClCompile Include="Source\A.c" />
    <ClCompile Include="Source\A_unittest.cc" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="main.h" />
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{7506F0B9-7FA3-4E1E-8EB5-A1819E232C59}</ProjectGuid>
    <RootNamespace>STACKOVERFLOW</RootNamespace>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <CharacterSet>MultiByte</CharacterSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>false</UseDebugLibraries>
    <WholeProgramOptimization>true</WholeProgramOptimization>
    <CharacterSet>MultiByte</CharacterSet>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup />
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>Disabled</Optimization>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>MaxSpeed</Optimization>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <IntrinsicFunctions>true</IntrinsicFunctions>
    </ClCompile>
    <Link>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <OptimizeReferences>true</OptimizeReferences>
    </Link>
  </ItemDefinitionGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
  <Target 
    Name="NormalBuild" 
    Condition=" '$(_InvalidConfigurationWarning)' != 'true' " 
    DependsOnTargets="_DetermineManagedStateFromCL;CustomBeforeBuild;$(BuildDependsOn)" 
    Returns="@(ManagedTargetPath)">
    <ItemGroup>
      <ManagedTargetPath Include="$(TargetPath)" Condition="'$(ManagedAssembly)' == 'true'" />
    </ItemGroup>
  </Target>

  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Test|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>

  <Target Name="Build"
    Returns="@(ManagedTargetPath)">
    <MSBuild Projects="STACKOVERFLOW.vcxproj" Targets="NormalBuild" Properties="Configuration=Test" />
  </Target>

  <Target Name="CustomBeforeBuild">
    <ItemGroup Condition="'$(Configuration)' == 'Test'">
      <ClCompile Remove="*main.c*" />
      <ClCompile Include="c:/gtest/*.c*" />
    </ItemGroup>
  </Target>

</Project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM