Unveil the secrets of Visual Studio Compilation: msbuild

Source: Internet
Author: User

We usually finish writingProgramAfter that, right-click build in Visual Studio and our program will be compiled. So what happened after clicking build? How can we compile without Visual Studio? This is the function provided by msbuild. The following describes its usage step by step using some examples.

Scenario:

There is a. CS file. You want to use msbuild to compile it into an EXE file.

UsingSystem;

 

NamespaceMyApp

{


Class
Program

{


Static
VoidMain (String[] ARGs)

{

}

}

}

 

Implementation:

1. Create a project file:

Create a. proj file (MyApp. proj in this example) in any text editor and add the following content to the file:

<Project defaulttargets = "compile"

Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003">

</Project>

This is a root node. In the subsequent steps, add some subnodes under it.

2. Add project properties.

A project has many attributes, such as the project name. Now add a name for this project:

<Project defaulttargets = "compile"

Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003">

<Propertygroup>

<Appname> MyApp </appname>

</Propertygroup>

</Project>

From aboveCodeAs you can see, all attributes are placed on the propertygroup node.

The appname attribute is actually similar to a variable. You can reference it elsewhere in the file through @ (appname.

Therefore, the nodes placed in propertygroup are equivalent to some global variables for use elsewhere in the file.

3. Add item.

Add the file to be compiled to the configuration below:

<Project defaulttargets = "compile"

Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003">

 

<! -- Set the application name as a property -->

<Propertygroup>

<Appname> MyApp </appname>

</Propertygroup>

 

<Itemgroup>

<Csfile include = "myclass. cs"/>

</Itemgroup>

</Project>

We can see that the item is placed in the itemgroup node. Csfile is similar to a variable, which can be referenced by @ (csfile) in other places of the file.

4. Add target.

Target refers to the set of tasks to be compiled, and is the task container.

<Project defaulttargets = "compile"

Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003">

 

<! -- Set the application name as a property -->

<Propertygroup>

<Appname> helloworldcs </appname>

</Propertygroup>

 

<Itemgroup>

<Csfile include = "myclass. cs"/>

</Itemgroup>

 

<Target name = "compile"> </Target>

</Project>

 

5. Add a task.

A task is a child node of a target. It is used to define a specific compilation task.

<Project defaulttargets = "compile"

Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003">

 

<! -- Set the application name as a property -->

<Propertygroup>

<Appname> helloworldcs </appname>

</Propertygroup>

 

<Itemgroup>

<Csfile include = "myclass. cs"/>

</Itemgroup>

 

<Target name = "compile">

<CSC sources = "@ (csfile)"> </CSC>

</Target>

</Project>

 

In the authorization code, cscis a reserved msbuildword, and csc.exe is used for compilation.

@ () Indicates the Set Name of the item. Here is the csfile item defined in step 1.If there are multiple csfile nodes in the current target, the CSC task will process them.

6. Add task output

Task output refers to the output settings of a specific task.

xmlns =" http://schemas.microsoft.com/developer/msbuild/2003 ">

helloworldcs

outputassembly =" ((appnamecmd.exe "

itemname = "myclass"/>

The taskparameter in the code above specifies the name of the output file, which uses the value of the outputassembly attribute defined in the CSC node.
Itemname specifies the name of this item. You can reference this item in other places using the @ () syntax.

7. Add a message

If you want to output some messages to indicate the compilation progress during the compilation process. You can add a message to a target.

<Project defaulttargets = "compile"

Xmlns = "http://schemas.microsoft.com/developer/msbuild/2003">

 

<! -- Set the application name as a property -->

<Propertygroup>

<Appname> MyApp </appname>

</Propertygroup>

 

<Itemgroup>

<Csfile include = "myclass. cs"/>

</Itemgroup>

 

<Target name = "compile">

<CSC sources = "@ (csfile )"

Outputassembly = "plugin (appnamecmd.exe">

<Output taskparameter = "outputassembly"

Itemname = "myclass"/>

</CSC>

<Message text = "the output file is @ (myclass)"/>

</Target>

</Project>

As you can see, this code references the itemname set in step 1.

 

Run:

    1. Put MyApp. proj and myclass. CS in a directory.

    1. Open the command line and execute msbuild ("C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ msbuild.exe "MyApp. proj). The execution result is as follows:
    2.  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.