I am planning to use libraries in my C++ program. Development is happening on Linux but application is designed to compile on both Linux and Windows. I understand direct equivalent for shared libraries(.so) in windows is DLL, right?
In Linux using g++, I can create shared library using -fPIC
and -shared
flags. AFAIK, there is no other code change required for a shared library. But things are different in a Windows DLL. There I should specify the functions which have to be exported using dllexport, right?
My question is how do I manage this situation? I mean dllexport is invalid in Linux and the compiler will give an error. But it is required in Windows. So how do I write a function which will compile on both platforms without any code change?
Compilers used
Any help would be great!
We specify __declspec(dllexport)
for class:
#define EXPORT_XX __declspec(dllexport)
class EXPORT_XX A
{
};
You can then check for platform and only define the macro on windows. E.g.:
#ifdef WIN32
#define EXPORT_XX __declspec(dllexport)
#else
#define EXPORT_XX
#endif
We mostly build static libraries so there might be more stuff to do for dynamic libs but the concept is the same - use preprocessor macro to define string that you need to insert into Windows code.
Another alternative is to just use a .def file for your windows project. This file specifies the DLL exports, so you won't have to mess up your code base. (But macros are definately the way to go if you want to avoid the extra file.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With