#using、#include 和“程序集引用” ——它们是什么以及它们之间有何关系?
我想知道 Visual Studio、其他 IDE 以及任何其他类型的情况(即根本没有 IDE)如何处理从外部引入代码。
起初我认为 #includes 是执行此操作的唯一方法,方法是将程序集文件放置在 Visual Studio 程序集文件的指定目录中,然后使用 <>格式来引入它们,或者通过将程序集文件放入项目目录中并使用“”格式来引入它们(即分别为<>和“”)。但现在我使用 #using 指令来讨论本文末尾的示例(需要注意的是,对于名称空间,该指令与不带“#”的“using”指令不同)。我还遇到过在 Visual Studio 中从“配置属性”对话框中添加程序集引用的情况。
那么,有人会告诉我向给定项目添加程序集文件和其他代码的所有细节吗?
--下面是让我困惑的例子--> 我的书中有这一部分指出:
"...该图结合了C++ 2008代码 具有遗留 C 和本机 C++ 代码。它 还介绍了两个组件 您最常使用的参考文件 与 C++ 2008 一起,以及他们的 关联的命名空间。与时不同 您使用 Visual Studio 开发一个 项目、程序集参考文件 默认情况下不包括在内 编写单个源文件。由于 那,你必须编写 #using 指令 对于这些文件。 ...”
#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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是本机 C++(通常简称为 C++),而是 C++/CLI,它实际上是一种 .NET 语言,旨在简化本机代码和托管代码之间的交互,因此可以同时使用两者。然而,它绝对不是 C++,尽管故意表现得很相似。程序集是 .NET 托管代码存储库。您可以使用
#using
命令来使用它们。#include
用于本机 C++ 标头。您还应该能够从项目的属性添加托管引用(即#using
但自始至终为您完成)。在本机 C++ 中,您必须
#include
标头,并且如果合适,链接到.lib
文件(或手动使用GetProcAddress
),并且 Visual Studio 还为 COM 库提供#import
。 C++/CLI 还提供#using
来引入托管程序集。如果您尚不了解 C++ 和 .NET 代码,和/或您不打算将两者链接在一起,则不建议使用 C++/CLI。
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 useGetProcAddress
manually), and Visual Studio also offers#import
for COM libraries. C++/CLI also offers#using
for bringing in managed assemblies.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.