How to export C-struct and typedefs from C++ dll ?

Started by
4 comments, last by Misery 10 years, 9 months ago

Hello,

I have a C souces that i want to wrap and export from dll library. For example:

C_lang_struct_and_enum.h:


typedef struct
{
  int x;
  double y;
} AStruct;

typedef enum
{
  a,
  b,
  c
} AnEnum;

I also have a few C - functions using them:

C_lang_functions.h:


void PrintAStruct(AStruct s,AnEnum value)
{
 /*print stuff*/
}

I want to export those functions with wrappers:

CppDLL.cpp:


namespace SomeNamespace
{
   void wrapPrintAStruct(wrapAStruct s,wrapAnEnum value) //wrapper
  {
      PrintAStruct(s,value);
  }
}

CppDLL.h:


#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif

namespace SomeNamespace
{
   typedef AStruct CPP_DLL_API wrapAStruct;
   typedef AnEnum CPP_DLL_API wrapAnEnum;
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

The DLL library is built succesfully, but I cannot use those symbols in a program as they seem to be undefined. How to workaround this? Any solution would be fine really.

Advertisement

Types are compile time, it doesn't make sense to load them dynamically at runtime from a DLL.

#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif
 
#include "C_lang_struct_and_enum.h"

namespace SomeNamespace
{
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

You need

extern "C" {

#include "C_lang_struct_and_enum.h"

}

to avoid the C names getting mangled by the C++ compiler though.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Types are compile time, it doesn't make sense to load them dynamically at runtime from a DLL.


#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif
 
#include "C_lang_struct_and_enum.h"

namespace SomeNamespace
{
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

But how to make those definitions remain in SomeNamespace?

If you need to wrap them too, then just add the typedefs again.

#ifdef CPP_DLL_EXPORTS
#define CPP_DLL_API __declspec(dllexport)
#else
#define CPP_DLL_API __declspec(dllimport)
#endif
 
#include"C_lang_struct_and_enum.h"

namespace SomeNamespace
{
   typedef AStruct wrapAStruct;
   typedef AnEnum wrapAnEnum;
   CPP_DLL_API void wrapPrintAStruct(AStruct s,AnEnum value);
}

thanks, I'll try :)

This topic is closed to new replies.

Advertisement