简体   繁体   中英

MSBuild looks for Main when building a project

I am working on a large project that consists of multiple executables. I'd like to automate deployment process by building all of them at once. That is why I resorted to msbuild command line utility.

When I build one of my projects in Visual Studio it build normally. When I try to do the same using msbuild cmd, it fails with an error

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [pathToLibrary.csproj]

This is cmd code:

msbuild MainProject.csproj -property:Configuration=Release -property:Platform="AnyCPU" -property:OutputType="WinExe" -target:"Rebuild"

pathToLibrary.csproj indeed is a library so I don't know why msbuild is trying to find a main method. There is none. It is a library.

What am I missing here?

pathToLibrary.csproj indeed is a library so I don't know why msbuild is trying to find a main method. There is none. It is a library.

When your main project references projects by Add Reference --> Projects , it will always build not only the main projects but also the referenced projects at the same time. So when you build that project by MSBuild command lines, -property:OutputType=winexe will also apply to these referenced projects. When you build the project in VS IDE and MSBuild, you will see these info in output log.

在此处输入图片说明

在此处输入图片说明

If there is no way to instruct msbuild to only overwrite OutputType for main .csproj, editing every file separately will have to do

If you just want to find a way to specify -property:OutputType=winexe to the main project not the referenced projects by MSBuild command line, I think there is no such function.

Or you could try my suggestions :

1) please remove -property:OutputType=winexe in MSBuild command line and when you create the related project, you have already specified the output type of the project, so you don't need to specify it in MSBuild which is not the job of the MSBuild.

Note that you can modify the property in xxx.csproj directly like <OutputType>WinExe</OutputType> .

2) if you still want this feature when you build the project with MSBuild command line, I suggest you could create a script called Directory.Build.targets which can overwrite the OutputType property and then build the project separately with MSBuild.

~a) please create a file called Directory.Build.targets in every project folder which the xxxx.csproj file exists.

~b) write the related property about the project in it:(use Exe in a console project ,use WinExe in a windows project and use Library in a class library project.)

 <Project>
    <Target Name="inputproperty" BeforeTargets="AssignProjectConfiguration">
    <PropertyGroup>
     <OutputType>WinExe</OutputType>
    </PropertyGroup>
    </Target>
    </Project>

write like this in your MainProject project and then create another Directory.Build.targets in your pathToLibrary project to use <OutputType>Library</OutputType> so that it will meet your expectations.

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