Here is how "Hello World" looks in Managed C++:
Listing 1. "Hello World" in Managed C++(HelloVC.cpp)
#using <mscorlib.dll> // Allow easy reference System namespace classes using namespace System; // Global function "main" is application's entry point void main() { // Write text to the console Console::WriteLine(S"Hello World using Managed C++!"); }
Even though the entire program is only a few lines of code, there are several things worth noticing, beginning with:
#using <mscorlib.dll>
In Managed C++, #using is similar to the #import directive (which is used to incorporate information from a type library). Note that these are different than the #include directive, which is for incorporating source code rather than pre-built libraries. Also, to import the namespace into the program (to make it convenient to reference System objects without having to fully qualify their path), an additional statement is required:
using System;
Classes can be either referenced fully (e.g. System.IO.FileStream, similar to a fully qualified path name) or their namespace can be imported into the program, after which it is not necessary to fully qualify the contained class names. For convenient access to System objects, that namespace must be imported:
void main() {…}
Though, in our example, the entry point main
takes no command line arguments, this can obviously be enhanced for non-trivial programs. Our entry point also doesn't return anything, though it's possible to modify the function to return a single 32-bit numeric value to be used as an exit code. The next line is:
Console::WriteLine(S"Hello World using Managed C++!");
The real meat of the program, this line outputs a string using the runtime Console type. The Console
type can be used for both input and output of any string or numeric value using Read
, ReadLine
, Write
, and WriteLine
methods. As mentioned above in the section "What Can Vary", the double-colon in Console::WriteLine
is required in C++ to indicate scope: It separates both a namespace from a class name as well as a class name from a static method. Finally, by specifying L
in front of the string, we tell the compiler to make this a Unicode string. Note that we can omit the L
and it will still compile, but this creates an ANSI string that will get converted at runtime into its Unicode equivalent (required by the runtime String class), which reduces performance. In general, the recommendation is to always use Unicode strings.
The file build.bat contains the single line necessary to build this program:
cl.exe /com+ HelloVC.cpp /link /entry:main
The first item of note is the /com+
switch, which tells the compiler to create managed code, as required by the NGWS runtime. Also important is the /entry:main
switch to indicate the entry point: This isn't required for traditional C++ programs which go through a more complicated initialization. Running this build file generates the following output:
C:\…\HelloWorld\vc>cl.exe /com+ HelloVC.cpp /link /entry:main Microsoft (R) 32-bit C/C++ Optimizing Compiler… Copyright (C) Microsoft Corp 1984-2000. All rights reserved. HelloVC.cpp Microsoft (R) Incremental Linker Version… Copyright (C) Microsoft Corp 1992-2000. All rights reserved. /out:HelloVC.exe /entry:main HelloVC.obj
Finally, running the resulting executable yields:
C:\…\HelloWorld\vc>hellovc Hello World using Managed C++!