WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
C++/CLI is a mechanism that offers .NET framework capabilities in C++ coding. If you are a genuine native C++ developer who is not aware of the numerous advantages offered by C++ coding under CLR platform,this article is especially crafted for you. You'll get a basic insight about C++/CLI programming under the .NET context by posting a first CLR console base project. You will also learn the common ways of compiling, executing, and debugging C++/CLI code.
You must have a moderate level of proficiency in native C++ programming code syntax manipulation, especially the understanding of code compilation, debugging, and execution under the native and CLR platforms. You also should have the latest version of the Visual Studio IDE installed and configured properly.
The Theory of C++/CLI
Figure 1: Operating system flow chart
So, C++/CLI is devised to rectify this common problem that eventually leads C++ code into disaster. C++/CLI is a version of the existing C++ programming language designed to run on the .NET Framework. It has been available since Microsoft Visual Studio 2005. The extension of native C++ code that is defined the by ANSI/ISO language, to incorporate the specific ECMA-72 standard is commonly referred to as C++/CLI code, to benefit the C++ language from the services that an implementation of CLI offers so that a programmer can use .NET constructs in existing C++ code. Finally, C++/CLI makes simple the common interoperability issue among languages by incorporating standard C++ code into .NET projects. Moreover, we have a .NET version of the C++ Standard Template Library (STL).
Getting Started with C++/CLI
The C++/CLI is an ideal programming language to migrate from native C++ into the .NET platform. This section provides the taste of the "Hello world" program in C++/CLI by introducing its first program under the Visual Studio IDE.
Create the Project
Step 1: First, open Visual Studio. I use Visual Studio 2013 Ultimate Edition. (Editor's note: You also can use Visual Studio Express 2013 for Windows Desktop.) Go to New project | select CLR from VC++ language template | choose CLR Console Application.
Figure 2: Starting a new project
Step 2: Assign a meaningful name to the project, such as hwTest, along with directory location.
Step 3: The C++/CLI solution creates multiple files such as header files, source, and so forth. This requires making an executable. Hence, hwTest.cpp is created and pastes the following code over there. Because the main() method used to be an entry point of an code execution., the "hello world" implementation is placed right here, as shown in Figure 3.
Figure 3: The "Hello, World" code
In the aforesaid code, .stdafx.h is a header file that contains essential definition information for the methods used in the program, whereas the System namespace imports the definition for methods used in the main method. Once you're done with this coding, save the entire solution. Next is the build and compilation phase that makes this program error free and yields the final executable.
Build and Compilation Phase
It is important to understand the difference between C++ compilation and C++/CLI compilation; the result of native C++ compilation is native code, whereas the result of C++/CLI compilation is managed code. However, you can compile that code on the command line without the Visual Studio build formal process, as shown in the following:
But, you'll get compiler errors because System is neither a class nor a namespace name, and the identifier WriteLine is not found. Both the namespace System and the method WriteLine are the managed aspects of your code. The Visual C++ compiler can act as a normal C++ compiler or as a C++/CLI compiler. By default, it remains a native compiler. To use it as a C++/CLI compiler, you use the compiler switch /clr, as in the following command line:
CL.exe /clr hwTest.cpp
Therefore, the hwTest.exe file created by the C++/CLI compiler and linker is a so-called .NET assembly. In fact, C++/CLI is a superset of the C++ language. A valid C++ program is also a valid C++/CLI program. As a consequence, your existing code base is not lost.
On the other side, if you don't opt the cl.exe utility to compile that code, the Visual Studio IDE formal build process (using F5) is sufficient to compile your code and make the final executable as following;
Figure 4: The results of the "Hello, World" program
Generated Project Files
After compilation, if you notice your project solution directory, there are a couple of other necessary files automatically created, apart from main.exe, as follows:
|.exe||This is the executable file for the program.|
|.obj||Compile produces these object files containing machine code from your program source files.|
|.ilk||Such files used by linker when you rebuild your project.|
|.pch||This is the pre-compiled header file.|
|.idb||They contain information that is used when we rebuild the application.|
|.pdb||It contains debugging information that is used when we execute the program in debug mode.|
This article has introduced some of the salient features of C++/CLI and shows you how to migrate native C++ code into CLR. By using C++/CLI, a native C++ developer can execute the code by leveraging the features of .NET CLR. Moreover, you can integrate managed code into existing C++ code. You have got to know that C++/CLI is extremely useful, both for extending existing applications and libraries with .NET features and for writing new .NET applications. This article provided an insight about the build and compilation of C++/CLI using both the cl.exe utility and the Visual Studio IDE. An experienced native C++ developer can enter into the managed C++ world by reading this article and incorporating the knowledge into his programming.