I am wanting to write some cross-platform library code.
I am creating a library both static and dynamic with most of the development done in Linux, I have got the static and shared library generated in Linux but now wanted to generate a Windows version of a static and dynamic library in the form of .lib
and .dll
using the same source code.
Is this possible? I'm a bit worried because I noticed generating Windows .dll
files required using _dllspec
or something similiar in your source code.
I am seeking the best and quickest solution to getting my code compiled on Windows. I don't need to do the compiling under Linux; I am happy to do it directly under Windows. Also I am using two external libraries which are Boost and Xerces XML which I have installed on both my Windows and Linux system so hopefully they shouldn't be a problem.
What I really want is to have a single source code copy that can be compiled under both Linux and Windows to generate libraries specific to each platform. I don't really care if I have to edit my code in favour of Windows or Linux as long as I can have a single source code copy.
DLLs are Microsoft's implementation of the idea of a "shared library." You can only use them on platforms that, in some fashion, implement support for them. In general, this means that no, you can't just take the DLL files you have and use them on Android, or on a macOS installation, or whatever.
New Visual Studio now officially supports CLang and GCC tool-chains along with its own compiler. However it doesn't support cross-compilation to Linux. Which, in turn, means that one still have to maintain at least two different projects in different IDEs to get native library binaries for all major operating systems.
C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS.
Libraries that exist as separate files outside of the executable files are known as dynamic libraries. At compile time the program makes one copy of the library's files.
In general, there are two issues you need to be concerned with:
__declspec(dllexport)
, andFor the first, you will need to learn about __declspec(dllexport)
. On Windows only projects, typically this is implemented in the way I describe in my answer to this question. You can extend this a step further by making sure that your export symbol (such as MY_PROJECT_API) is defined but expands to nothing when building for Linux. This way, you can add the export symbols to your code as needed for Windows without affecting the linux build.
For the second, you can investigate some kind of cross-platform build system.
If you're comfortable with the GNU toolset, you may want to investigate libtool (perhaps in conjunction with automake and autoconf). The tools are natively supported on Linux and supported on Windows through either Cygwin or MinGW/MSYS. MinGW also gives you the option of cross-compiling, that is, building your native Windows binaries while running Linux. Two resources I've found helpful in navigating the Autotools (including libtool) are the "Autobook" (specifically the section on DLLs and Libtool) and Alexandre Duret-Lutz's PowerPoint slides.
As others have mentioned, CMake is also an option, but I can't speak for it myself.
You can pretty easily do it with #ifdef's. On Windows _WIN32 should be defined by the compiler (even for 64 bit), so code like
#ifdef _WIN32 # define EXPORTIT __declspec( dllexport ) #else # define EXPORTIT #endif EXPORTIT int somefunction();
should work OK for you.
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