从非托管C ++创建DLL [英] Create DLL from unmanaged C++
问题描述
我目前有一个以非托管C ++编写的控制台应用程序,源代码由入门点 main
和其他一些功能组成。我需要从这个代码创建一个DLL,以便我可以使用其他项目,特别是从托管的C ++。 (另一个问题:为此,我必须编写一个包装类吗?)
I currently have a console application written in unmanaged C++, the source code consists of an entry-point main
and some other functions. I need to create a DLL from this code so that I can use it from other projects, specifically from managed C++. (Another question: would I have to write a wrapper class for this purpose?)
正如我所知,托管/非托管C ++和创建DLL没有任何意义,我遵循< a href =http://msdn.microsoft.com/en-us/library/ms235636(v=vs.80).aspx =nofollow noreferrer>本教程,并设法获得一个简单的Hello World DLL通过使用VS2010(没有CMake)启动并运行。
As I know next to nothing of managed/unmanaged C++ and creating DLLs, I followed this tutorial and managed to get a simple Hello World DLL up and running by using only VS2010 (no CMake).
然而,我的这个项目有很多依赖(例如点云库),所以我通常使用CMake生成Visual Studio 2010解决方案,然后构建为可执行文件,如 PCL教程。如何使用CMake构建一个将构建为DLL的VS2010项目?
However, this project of mine has a lot of dependencies (e.g. Point Cloud Library), and so I normally use CMake to generate the Visual Studio 2010 solution that then builds into an executable, as described in the PCL Tutorial. How can I use CMake to build a VS2010 project that will build into a DLL?
总结我的问题:
- 我有一个非托管C ++代码的项目需要很多依赖。
- 我想从这个代码中创建一个DLL,可以从管理C ++。
额外的信息:
Windows 7,Visual Studio 2010 Ultimate,CMake 2.8.10.2
Extra information: Windows 7, Visual Studio 2010 Ultimate, CMake 2.8.10.2
编辑:
我使用CMake与你的行改变,它的工作正常。这是我添加到我的头文件中,我在正确的轨道上?
I used CMake with your line changed, and it worked as expected. This is what I have since added to my header file, am I on the right track?
MyCode.h
#ifdef MyLib_EXPORTS
#define API_DECL __declspec( dllexport )
#else
#define API_DECL __declspec( dllimport )
#include <iostream>
#include <pcl/...>
etc...
API_DECL void myFirstFunction();
API_DECL void mySecondFunction();
#endif
MyCode.cpp:我没有对源文件进行任何更改,我应该做什么?
MyCode.cpp: I have not made any changes to the source file, should I make any?
推荐答案
不幸的是,我无法帮助您使用托管代码部分,但这是您如何创建一个DLL在CMake中:
Unfortunately, I cannot help you with the managed code part, but this is how you create a DLL in CMake:
首先,而不是使用
`ADD_EXECUTABLE( YourLib SHARED yourclass.cpp yourclass.h )`
.txt ,使用
in your CMakeLists.txt
, use
`ADD_LIBRARY( YourLib SHARED yourclass.cpp yourclass.h )`
这将配置解决方案来创建一个DLL而不是可执行文件。
This will configure the solution to create a DLL rather than an executable.
但是,为了能够将这个DLL与您的项目一起使用,您需要导出要使用的符号。为此,您需要将 __ declspec(dllexport)
添加到您的类和/或函数声明中。然后构建库将产生两个文件,一个。 dll
和一个 .lib
。后一种是您想在其他项目中使用此库时所需的所谓导入库。运行时将需要 .dll
。
However, to be able to use this DLL with your projects, you need to export the symbols you want to use. To do this, you need to add __declspec( dllexport )
to your class and/or function declarations. Building the library will then yield two files, a .dll
and a .lib
. The latter one is the so-called import library that you need when you want to use this library in your other projects. The .dll
will be required at runtime.
但是:当您想使用库时,需要使用 __ declspec(dllimport)
(而不是 dllexport
)。为了避免使用两个头文件,通常的方法是使用预处理器。在您的库项目中提供一个 YourLibrary_EXPORTS
define
可以帮助您。
However: When you want to use your library, you need to use __declspec( dllimport)
(rather than dllexport
). To avoid using two header files, the usual way to do this is to use the preprocessor. CMake actually helps you by providing a YourLibrary_EXPORTS
define
in your library project.
总结:
#ifndef YOUR_CLASS_H
#define YOUR_CLASS_H
#ifdef YourLib_EXPORTS
#define API_DECL __declspec( dllexport )
#else
#define API_DECL __declspec( dllimport )
#endif
class APIDECL YourClass {
void foo();
void bar();
};
#endif // YOUR_CLASS_H
编辑:
如果你想要能够使用C(和能够使用C函数的语言)的这些函数,你应该用 externC{
extern "C" {
API_DECL void myFirstFunction();
API_DECL void mySecondFunction();
}
这篇关于从非托管C ++创建DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!