Creating DLLs from Unmanaged C ++

I currently have a console application written in unmanaged C ++, the source code consists of an entry point mainand some other functions. I need to create a DLL from this code so that I can use it from other projects, in particular from managed C ++. (Another question: should I write a wrapper class for this purpose?)

Since I know almost nothing about managed / unmanaged C ++ and creating a DLL, I followed this tutorial and managed to get a simple Hello World DLL that starts and works using only VS2010 (without CMake).

However, my project has many dependencies (e.g. Cloud Cloud Library ), and therefore I usually use CMake to create Visual Studio 2010, which is then embedded in the executable, as described in the PCL Tutorial . How can I use CMake to create a VS2010 project that will be embedded in a DLL?

To summarize my problem:

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

Additional Information: Windows 7, Visual Studio 2010 Ultimate, CMake 2.8.10.2

EDIT: I used CMake with the modified string, and it worked as expected. Is this what I added with 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: , -?

+5
1

, , DLL CMake:

,

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

CMakeLists.txt,

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

DLL, .

, DLL , , . __declspec( dllexport ) / . :. dll a .lib. - , , . .dll .

. , __declspec( dllimport) ( dllexport). , - . CMake , YourLibrary_EXPORTS define .

:

#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: C ( , C), extern "C" {

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

All Articles