我有一行代码,我从一个名为Zetcode的网站,并在此构建第一个例子,我编译代码如下:
gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll 01-simple-cs-example.cs -out:simple-sample.exe
这构建了绘制我的窗口的exe,但我不认为我的csproj
文件是正确的。
<Project DefaultTargest="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- This Property will make a variable named MSBuildSample, then you can use it to
assign to whatever in the script.
Properties are variables inside the script.
-->
<SrcPath>src</SrcPath>
<AssemblyName>01-simple-cs-example</AssemblyName>
<OutputPath>bin</OutputPath>
</PropertyGroup>
<ItemGroup>
<!-- This Compile statement, gathers all the source files listed in an item called Compiled
ItemGroups interact with the source code.
-->
<Compile Include="$(SrcPath)01-simple-cs-example.cs" />
</ItemGroup>
<!-- In the Build target, the Inputs and Outputs targets which looks to see if the files have been updated,
or if the files are existent. If the files have not been changed, then the Build target is skipped.
-->
<Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe" >
<!-- The MakeDir directive will create the directory in the property group above, if it meets the
condition stated in the Condition= statement.
-->
<Message Text="Creating the output path $(OutputPath)." />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
<!-- This Csc is the .NET C# compiler, which then uses the ItemGroup of collected sources called, Compile
-->
<Message Text="Compiling the source code." />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
</Project>
这是尝试运行xbuild和msbuild的结果。
C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example>xbuild
XBuild Engine Version 3.3.0.0
Mono, Version 3.3.0.0
Copyright (C) Marek Sieradzki 2005-2008, Novell 2008-2011.
Build started 7/25/2014 2:47:41 PM.
__________________________________________________
Project "C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example 1-simple-cs-example.csproj" (default target(s)):
Target Build:
Created directory "bin"
Tool C:monoMono-3.2.3bingmcs.bat execution started with arguments: /out:bin 1-simple-cs-example.exe src 1-simple-cs-example.cs
src 1-simple-cs-example.cs(1,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
src 1-simple-cs-example.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
src 1-simple-cs-example.cs(4,23): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
Task "Csc" execution -- FAILED
Done building target "Build" in project "C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example 1-simple-cs-example.csproj".-- FAILED
Done building project "C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example 1-simple-cs-example.csproj".-- FAILED
Build FAILED.
Errors:
C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example 1-simple-cs-example.csproj (default targets) ->
(Build target) ->
src 1-simple-cs-example.cs(1,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
src 1-simple-cs-example.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
src 1-simple-cs-example.cs(4,23): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
0 Warning(s)
3 Error(s)
Time Elapsed 00:00:01.5341534
这是MSBuild运行的结果。MSBuild成功完成,但我想这是因为MSBuild知道在哪里寻找,而无需我在csproj
文件中指定它。
C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example>MSBuild
Microsoft (R) Build Engine version 4.0.30319.18408
[Microsoft .NET Framework, version 4.0.30319.18444]
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 7/25/2014 2:48:04 PM.
Project "C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example 1-simple-cs-example.csproj" on node 1 (default targets).
Build:
C:WindowsMicrosoft.NETFrameworkv2.0.50727Csc.exe /out:bin 1-simple-cs-example.exe src 1-simple-cs-example.cs
Done Building Project "C:UsersUser01Dropboxprogrammingcsharpzetcode-csharp-winforms 1-simple-cs-example 1-simple-cs-example.csproj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:04.36
我不知道该把什么放入csproj
文件中,使其加载我在命令行中使用gmcs
编译的那些DLL。有线索吗?
—编辑2014-08-20—
在使用了knockte的答案后,我能够看到References
是如何添加到csproj
文件中的。它就像添加一个项目组一样简单,并在c#源文件中添加您用using
列出的引用。
<ItemGroup>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Drawing" />
<Reference Include="System" />
</ItemGroup>
如果您正在手动构建csproj
文件,这是手动方式。
只需使用IDE (MonoDevelop或Visual Studio)添加gac引用到System.Windows.Forms
和System.Drawing
库。