Building a simple NetCF application from the command line - C# version

Now that NetCF is part of the .NET Framework v2 SDK, I get asked how to build applications using the command line tools. Today I'm going to show how to build a simple (single source file) C# application using csc.exe.

For more complicated applications (more than one source file, building DLL assemblies at the same time as your application, including resources, etc), I recommend creating a makefile and using nmake.exe (or your favorite make system).

To make your life simpler, I suggest creating an environment variable to store the path to the .NET Compact Framework assemblies (mscorlib.dll, system.dll, etc). The default path (as installed by Visual Studio or the SDK) is long and can get cumbersome to retype for each assembly you reference. On my machine, I set the environment variable is as follows:

set NETCFLIBS="c:\program files\microsoft.net\sdk\v2.0\compactframework\windowsce"

You can add this line to your sdkvars.bat file (typically installed in c:\program files\microsoft.net\sdk\v2.0\bin) so that it gets set every time you run the .NET Framework SDK Command Prompt. Also, be sure to use quotation marks around the path. Since the default path contains a space, forgetting the quotes will cause the command line compiler to fail to find your assembly references.

Once NETCFLIBS has been set, you can build your application from within the .NET Framework SDK Command Prompt quite easilly. The example below shows the command line to build a single source file WinForms application that uses Web Services. In this example, the non-bold text specifies your source file and the name of the resulting executable.

csc.exe /noconfig /nostdlib /target:exe /out: SimpleApp.exe /r:%NETCFLIBS%\mscorlib.dll /r:%NETCFLIBS%\system.dll /r:%NETCFLIBS%\system.drawing.dll /r:%NETCFLIBS%\system.windows.forms.dll /r:%NETCFLIBS%\system.web.services.dll Form1.cs

You can use csc /? to decipher the compiler switches, but a couple bear mentioning.

/noconfig
This switch instructs the compiler to not include the default response file (csc.rsp). This file, if present, sets a handful of default switches for the compiler that are not applicable when building NetCF applications.

/nostdlib
This switch instructs the compiler to not include it's standard list of references. When building NetCF projects, you need to use the .NET Compact Framework assemblies and not those of the desktop framework.

Forgetting to include either of these switches will generate compile time errors regarding duplicate references.

I hope this helps you get your C# applications building! I plan to have a Visual Basic.NET equivalent command line posted sometime in the not too distant future.

Take care,
-- DK

Disclaimers:
This posting is provided "AS IS" with no warranties, and confers no rights.
Some of the information contained within this post may be in relation to beta software. Any and all details are subject to change.