Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create DLL from unmanaged 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?)

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?

like image 651
hjweide Avatar asked Jan 22 '13 15:01

hjweide


1 Answers

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();
}
like image 110
Johannes S. Avatar answered Sep 28 '22 05:09

Johannes S.