API macro for making C++ library

Viewed 320

I am a beginner in C++.

I have created a CMake library project, and I added 2 libraries, one is called mosaic (shared) and mosaic_s (static).

And the problem is that how can I use macros to automatically set a macro for __declspec(dllexport) when building shared library and set it to nothing when building a static one.

1 Answers

Define a symbol in your make file that will tell you if it is shared or not and then you can use the preprocessor like

#ifndef SHARED
#define EXPORT
#else
#define EXPORT __declspec(dllexport)
#endif

and then in your code you would have

EXPORT whatever_you_are_exporting

and if you have defined SAHRED in your make file then EXPORT will be __declspec(dllexport) and if you have not then EXPORT will become nothing.

Related