Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#using, #include, and 'assembly references' -- what are they and how do they relate?

I'm wondering how Visual Studio, other IDE's, and any other sort of circumstances (ie. no IDE at all) handle bringing in code from outside.
At first I thought #includes were the only way to do this, by either placing the assembly files in the designated directory for Visual Studio assembly files and then using the <> format to bring them in, or by putting the assembly files in the project directory and using the "" format to bring them in (that is, <> and "" respectively). But now I come up to the example at the end of this post with the #using directive (which, to note, is different than just the 'using' directive without the '#', for namespaces). Also I've come across adding assembly references in visual studio from within the 'configuration properties' dialogue.
So, would someone set me straight on all the in's and out's of adding assembly files and other code to a given project?

--The following is the example that has confused me--> I have this section in my book that states:

"...The figure combines C++ 2008 code with legacy C and native C++ code. It also presents the two assembly reference files you'll use most often with C++ 2008, along with their associated namespaces. Unlike when you use Visual Studio to develop a project, the assembly reference files aren't included by default when you code a single source file. Because of that, you must code #using directives for these files. ..."

#include <stdio.h>
#include <iostream>
#using   <system.dll>
#using   <system.windows.forms.dll>

// Associated namespace directives

using namespace std;
using namespace System;
using namespace System::Windows::Forms;

void main()
{
    printf(            "Hello, Earth\n");  // from stdio.h
    cout <<            "Hello, Mars\n";    // from iostream
    Console::WriteLine("Hello, Jupiter");  // from system.dll
    MessageBox::Show  ("Hello, Saturn");   // from system.windows.forms.dll
}
like image 843
Evan Sevy Avatar asked May 23 '11 17:05

Evan Sevy


1 Answers

This is not native C++ (usually just referred to as C++), but C++/CLI, which is actually a .NET language designed to ease interacting between native and managed code, and as such can use both. It is, however, definitely not C++, despite an intentionally strong resemblance. Assemblies are .NET managed code repositories. You use the #using command to use them. #include is for native C++ headers. You should also be able to add managed references (that is, #using but done throughout for you) from the project's properties.

In native C++, then you must #include headers, and if appropriate, link to .lib files (or use GetProcAddress manually), and Visual Studio also offers #import for COM libraries. C++/CLI also offers #using for bringing in managed assemblies.

void main()
{
    printf(            "Hello, Earth\n");  // C native code
    cout <<            "Hello, Mars\n";    // C++/CLI's wrapper on C++ Standard
    Console::WriteLine("Hello, Jupiter");  // .NET managed code
    MessageBox::Show  ("Hello, Saturn");   // A thin wrapper on WinAPI
}

If you don't already know both C++ and .NET code, and/or you're not trying to link the two together, it's not recommended to use C++/CLI.

like image 117
Puppy Avatar answered Sep 28 '22 04:09

Puppy