The problem of mixing c and C ++

I need to create a C ++ project that exports functions to a project c this is my C ++ class:

** MyCppClass.h **

class MyCppClass
{
public:
static void MyCppMethod()
}

** MyCppClass.cpp **

void MyCppClass::MyCppMethod(){}

* Now I need to create an interface for the MyCppMethod (static) method.

I did this: ** MyExport.h **

#define Export __declspec(dllexport)
extern "C" void Export MyCppMethodWrapper();

** MtExport.cpp **

#include "MyCppClass.h"
#include "MyExport.h"

void MyCppMethodWrapper() { MyCppClass::MyCppMethod();}

here it is!

now part C (another project) I linked the project with MyExport.lib
** program.c **

#include "MyExport.h"        ->does not compile because of the extern "C"
int main()
{
  MyCppMethodWrapper();   

}

if I don’t add a line: #include "MyExport.h"in program.c the program compiles and works fine, but I need to provide a header for export (the client needs a header), and I want the program to use this header. how can i solve this ???

thank you for your responses

+3
source share
4 answers

extern, , :

#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif

:

EXTERN_C Export MyCppMethodWrapper();
+5

, C- ++, extern "C". .

, dll, extern ... C-, .

, ++ , / , , . Linux, , MS Windows, , -.

- , .

+1

: C ++, !

++ C, pure-C (.. extern "C" { ... } , ++ ( , , C) C . :

#ifdef __cplusplus // defined at least by G++, I don't know for other compilers
extern "C" {
#endif

// ... function definitions here
#ifdef __cplusplus
}
#endif
+1

brainiac, dllimport program.c

/* declare function directly */
extern "C" void __declspec(dllimport) MyCppMethodWrapper();

int main()
{
  MyCppMethodWrapper();   

}

, brainiac #ifdef __declspec(dllexport)

#ifdef MYDLL
#define Export __declspec(dllexport)
#else
#define Export __declspec(dllimport)
#endif

EXTERN_C void Export MyCppMethodWrapper();

/D MYDLL dll.

0

Source: https://habr.com/ru/post/1732825/


All Articles