从 Visual C 导出非托管类 DLL?
当使用 Visual C++ 2008 创建 DLL 时,我有几个选择。 我可以创建一个“类库”,据我所知,它实际上会给我一个使用 C++ 的 CLI(托管)扩展的 .Net 库。
因为我不希望这样,并且我假设我需要一个静态 .LIB 文件来链接到另一个 Visual C++ Windows 可执行项目,所以我选择“Win32 项目”,并在“应用程序设置”面板上指定 C++(无 MFC) ) DLL。
这将创建一个带有 .cpp 文件的项目,该文件应该是我定义“DLL 应用程序的导出函数”的位置。
这似乎也不是我想要的。 基本上,我正在寻找的是与 C# .NET 中的类库程序集等效的本机 C++。 我想将一些类打包到 DLL 中,然后让 .EXE 项目通过包含 DLL 项目头文件并链接到 .LIB 来使用 DLL 的类来解析引用。
这样做的通常方法是什么?
When creating a DLL with Visual C++ 2008 I have a couple of choices. I can create a "Class Library", which I understand will actually give me a .Net Library that uses the CLI (managed) extenstion of C++.
Since I don't want that, and I assumed that I need a static .LIB file to link into another Visual C++ windows executable project, I choose instead "Win32 Project" and, on the Application Settings panel, specify a C++ (no MFC) DLL.
This will create a project with a .cpp file which is supposed to be where I define "the exported functions for the DLL application".
This doesn't seem to be what I want either. Basically, what I'm looking for is the native C++ equivalent of what would, in C# .NET be a class library assembly. I want to package some classes into a DLL, then have a .EXE project use the DLL's classes by including the DLL project header files and link with a .LIB to resolve references.
What's the usual way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你制作一个 C++(非 MFC)DLL 是对的。 您可以创建您的类,并且您定义的那些入口点将从该 DLL 导出以供其他 C++ 代码使用(例如,用 C++ 编写的 Win32 应用程序)。
由于 C++ 名称会被编译器自动修改为奇怪且美妙的值,因此如果 DLL 的客户端是 C 程序,则按原样导出它们是不切实际的。 但如果一切都是用 C++ 编写的,那么应该没问题。
如果您创建一些类,您可以选择将它们动态链接(作为 DLL),但您将需要一个包含 DLL 符号定义的导入库(自动为您创建)。 您还可以选择从应用程序静态链接到您的代码 - 在这种情况下,您最终会得到一个静态库(也是 .LIB),其中包含类中的实际目标代码而不是 DLL 中的符号。
当然,DLL 的优点是,如果您使用自己的库编写多个应用程序,它们都可以共享该 DLL; 对于静态库,它们每个都包含库代码的副本。
You are right to make a C++ (no MFC) DLL. You can create your classes and those entry points which you define will be exported from that DLL for use by other C++ code (for example, a Win32 application written in C++).
Since C++ names get mangled automatically by the compiler to weird and wonderful values, it's not practical to export them as is if the DLL's clients are, for example, C programs. But if everything is in C++, you should be OK.
If you create some classes, you can choose to have them linked dynamically (as a DLL) but you will need an import library (created for you automatically) which contains the DLL's symbol definitions. You can also choose to link statically to your code from an application - in this case you would end up with a static library (also a .LIB) which contains the actual object code in your classes rather than symbols in a DLL.
The advantage of a DLL is, of course, that if you write several applications using your library, they can all share the DLL; with a static library, they would each contain a copy of your library code.
我认为这篇文章描述了您正在尝试做的事情:
http://www.codeproject.com/KB/mcpp/usingcppdll.aspx
就我个人而言,我也更喜欢导出 C 函数(而不是 C++),其中我显式地显示 this 指针,以避免必须关心编译器特定的方法名称修饰和公开编译器生成的函数。
I think this article describes what you are trying to do:
http://www.codeproject.com/KB/mcpp/usingcppdll.aspx
Personally I also prefer exporting C functions (as opposed to C++) where I make the this pointer explicit to avoid having to care about compiler specific method name decoration and exposing compiler generated functions.
你做对了。 您需要用 __declspec(dllexport) 使它们可以从项目外部使用。 构建项目时,您将生成 .DLL 和 .LIB。
You're doing it right. What you'll need is to mark your classes with __declspec(dllexport) to make them available from outside the project. When you build the project, you'll generate both a .DLL and a .LIB.
当您生成项目时,它将为您存根导出的类,通常名为 C{MyLib}。
When you generate the project, it will stub out an exported class for you, typically named C{MyLib}.