I’m using MSVC, but I plan to make my library cross platform.
Right now, I have h and cpp files of classes and functions. Is making a library in MSVC as simple as adding all my files to a Visual C++ Project marked as either Static or Dynamic Library? Or must I make my classes and functions extern and whatnot?
Thanks
In the case of DLLs, you’ll want to specify
__declspec(dllimport)or__declspec(dllexport)based on whether you’re building a DLL or importing it. Consider this example:NOTE: I reordered “void” and “DLL_EXPORT” (formerly DLL_FUNCTION) as I had it mixed up before.
When including the header from within the DLL project, functions will be marked for export. When including it from another project (that doesn’t have the BUILDING_DLL preprocessor definition) it will be interpreted as a DLL import. This can be applied to classes in exactly the same manner. On Linux (and possibly other similar platforms), you can safely define DLL_FUNCTION to be nothing at all, as it is not required.
Edit: and if you want your functions to be callable from C you’ll obviously want to throw in ye olde
extern "C"as well, to prevent the name mangling.Edit 2: in practice, MSVC will generate a static library, too, when building a DLL. Any project that uses the DLL must link with the library (which doesn’t actually contain the executable code; it’s still dynamically linked as one would expect) to satisfy the linker.