Вопрос

I am creating a c++ dll to be imported in vb.net.

I have found that the best way to organize the imports and exports in the header file of the dll, is something of the following type:

#ifndef MY_DLL_EXPORTS
    #define MY_DLL_EXPORT __declspec(dllexport)
#else
    #define MY_DLL_EXPORT __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
#endif

    MY_DLL_EXPORT BOOL my_function(uint32_t x);

#ifdef __cplusplus
}
#endif

My question is, where are the __cplusplus and MY_DLL_EXPORTS supposed to be defined ?

I have not done this before and I can't seem to find a place in the code for it. I read something about putting these definitions in the

Project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

Do I put them in the DLL's project properties ? and if i do, on using this DLL, will they always be defined so the if statement will always go to the dllimport ?

Also, is __cplusplus already defined or must I define it ?

Это было полезно?

Решение

__cplusplus is defined for you by a C++ compiler, but NOT by a C compiler. This is how you can determine if your code is being compiled by a C or C++ compiler. That's why this is used to guard the extern "C" construct. A C compiler would just omit it (since it emits C linkage already).

The other constants can be either defined on the compiler's command line (eg. in the menu you listed), or in the code somewhere:

#define MY_DLL_EXPORTS

Другие советы

Best place to define COMPILING_DLL is command line of compiler. If you use Visual Studio IDE then it is in Project properties ... C/C++ ... Preprocessor ... Preprocessor Definitions.

__declspec(dllimport) is Microsoft specific extension to C++. Microsoft has excellent online documentation.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top