从非托管C ++创建DLL [英] Create DLL from unmanaged C++

查看:125
本文介绍了从非托管C ++创建DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个用非托管C ++编写的控制台应用程序,源代码由一个入口点 main 和一些其他函数组成。我需要从这个代码创建一个DLL,以便我可以使用它从其他项目,特别是从托管C ++。 (另一个问题:为此我需要写一个包装类吗?)



由于我知道托管/非托管C ++和创建DLLs没有什么, a href =http://msdn.microsoft.com/en-us/library/ms235636%28v=vs.80%29.aspx =nofollow>本教程,并设法获得一个简单的但是,我的这个项目有很多依赖(例如

< //pointclouds.org/rel =nofollow>点云库
),所以我通常使用CMake生成Visual Studio 2010解决方案,然后构建成一个可执行文件,如 PCL教程



总结我的问题:


  1. 我有一个非托管C ++代码的项目,需要很多依赖关系。

  2. 我想从这个代码创建一个DLL,可以从托管C ++。

额外信息:
Windows 7,Visual Studio 2010 Ultimate,CMake 2.8.10.2



编辑:
我使用CMake与您的行更改,并按预期工作。



MyCode.h

  #ifdef MyLib_EXPORTS 
#define API_DECL __declspec(dllexport)
#else
#define API_DECL __declspec(dllimport)

#include< iostream>
#include< pcl /...& gt
etc ...

API_DECL void myFirstFunction();
API_DECL void mySecondFunction();
#endif

MyCode.cpp:我没有对源文件进行任何更改,不好意思,我不能帮助你管理代码部分,但这是你如何创建一个DLL in CMake:



首先,不使用

 在您的 CMakeLists中的ADD_EXECUTABLE(YourLib SHARED yourclass.cpp yourclass.h)`

.txt ,使用

 `ADD_LIBRARY(YourLib SHARED yourclass.cpp yourclass.h)`

这将配置解决方案来创建DLL而不是可执行文件。



但是,为了能够在项目中使用此DLL,您需要导出要使用的符号。为此,您需要向您的类和/或函数声明中添加 __ declspec(dllexport)。然后构建库将产生两个文件,a。 dll .lib 。后者是所需的导入库,当你想在其他项目中使用这个库。在运行时将需要 .dll



:当您想使用您的库时,您需要使用 __ declspec(dllimport)(而不是 dllexport )。为了避免使用两个头文件,通常的做法是使用预处理器。 CMake实际上通过在您的库项目中提供 YourLibrary_EXPORTS define 来帮助您。



总结:

  #ifndef YOUR_CLASS_H 
#定义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{

  externC{
API_DECL void myFirstFunction();
API_DECL void mySecondFunction();
}


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?)

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).

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?

To summarise my problem:

  1. I have a project of unmanaged C++ code that needs a lot of dependencies.
  2. I want to create a DLL from this code that can be called from managed C++.

Extra information: Windows 7, Visual Studio 2010 Ultimate, CMake 2.8.10.2

EDIT: 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: I have not made any changes to the source file, should I make any?

解决方案

Unfortunately, I cannot help you with the managed code part, but this is how you create a DLL in CMake:

First of all, instead of using

`ADD_EXECUTABLE( YourLib SHARED yourclass.cpp yourclass.h )` 

in your CMakeLists.txt, use

`ADD_LIBRARY( YourLib SHARED yourclass.cpp yourclass.h )`

This will configure the solution to create a DLL rather than an executable.

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.

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.

To summarize:

#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

EDIT: If you want to be able to use those functions from C (and languages that are able to use C functions) you should wrap your declarations with extern "C" {

extern "C" {
    API_DECL void myFirstFunction();
    API_DECL void mySecondFunction();
}

这篇关于从非托管C ++创建DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆