Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross platform dynamic linking (Windows to linux)

I have an application which I have managed to keep reasonably cross-platform between windows and Linux. Cmake and Boost have been helpful in this endeavor.

The time has come to link to a .dll which has been written for windows. If I can put off the conversion of the dynamically linked library it would be a good thing. Other windows applications connect up to this library like this:

HINSTANCE dll;
dll = LoadLibraryA(filename);
//...
libFuncPtr = (libFuncPtr)GetProcAddress(dll, "libFunc");

I'd like to know if there are generic analogs to these functions or is it time to start dropping in my system specific preprocessor directives? The Current Priority for Developemt is Windows, It's understood there will have to be a Linux port of the library for the Linux build to work, but I'd like to keep the calling code as generic as possible.

like image 773
2NinerRomeo Avatar asked Mar 12 '12 21:03

2NinerRomeo


People also ask

How we use dynamic linking in Linux?

Dynamic libraries are linked during the execution of the final executable. Only the name of the dynamic library is placed in the final executable. The actual linking happens during runtime, when both executable and library are placed in the main memory.

Are DLL cross-platform?

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.

Can Msvc cross compile?

Build supports cross compilation with the gcc and msvc toolsets. For the complete list of allowed opeating system names, please see the documentation for target-os feature. When using the msvc compiler, it's only possible to cross-compiler to a 64-bit system on a 32-bit host.

Can Visual Studio cross compile Linux?

Linux projects are available in Visual Studio 2017 and later. First, make sure you have the Linux Development Workload for Visual Studio installed. For more information, see Download, install, and setup the Linux workload. For cross-platform compilation, we recommend using CMake.


2 Answers

Start dropping system specific preprocessor directives.

You could write an intermediary interface to provide functions like LoadLib(), LoadSym(), etc, that calls the aproppriate native API. In other words, when compiling for Windows LoadLib() will use LoadLibraryA() and on Linux it will use dlopen().

This will let you hide this ugly part of the code from your main code so it stays more readable.

like image 90
karlphillip Avatar answered Sep 24 '22 05:09

karlphillip


A good example of such a class is Qt's QLibrary, which has a uniform interface for loading dynamic libraries into programs.

see http://qt-project.org/doc/qt-4.8/qlibrary.html for more details.

Since Qt is cross platform, it can be used everywhere. Since Qt is open source, you can have a peek at the code. I would not use it directly if you don't need other stuff from Qt, as it comes with a big linkage baggage by itself.

I remember implementing something along these lines in the past, but the code is long gone.

like image 26
Not_a_Golfer Avatar answered Sep 20 '22 05:09

Not_a_Golfer