Question

The following main wants to be compiled via msbuild

#include <stdio.h>

int main (int argc, char *argv[])
{
    char Buffer[2000];
    printf("TEST");
    gets(Buffer);
}

A Batch file calls msbuild:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild main.vcxproj
pause

The msbuild project file is as follows.

<?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>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ItemGroup>
    <ClCompile Include="main.c" />
  </ItemGroup>
</Project>

The whole setup works so far but i need to find a good source to help me learn more about msproject files. I want to learn multiple things. How can I:

  • get rid of the "Import" oprtions and define the imported parts myself
  • use a commandline parameter to compile a set of files... ( %param%.c %param%TEST.c ,...)
  • use the projectfile to launch programs immediatelly ( %param%Test.exe )
  • compile multiple targets,... release, debug, etc
  • copy after compiling : Subproject/release/%param%.obj -> c:\accurateVersion\%param%.obj
  • Clean before rebuilding
Was it helpful?

Solution

There is a great book with lots of sample code to do everything you are talking about.

Inside the Microsoft® Build Engine: Using MSBuild and Team Foundation Build

Here is how you can call clean and then the build targets and also have it compile the the debug and release configurations. C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild main.vcxproj /t:clean;build /p:Configuration=Debug;Release /p:platform=Win32

The copy task:

<ItemGroup>
    <MySourceFiles Include="c:\MySourceTree\**\*.*"/>
</ItemGroup>

<Target Name="CopyFiles">
    <Copy
        SourceFiles="@(MySourceFiles)"
        DestinationFiles="@(MySourceFiles->'c:\MyDestinationTree\%(RecursiveDir)%    (Filename)%(Extension)')"
 />

This link might help you with some of your other questions.

Walkthrough: Using MSBuild to Create a Visual C++ Project

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top