且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

跨平台动态链接(Windows到linux)

更新时间:2023-11-23 09:12:52


开始删除系统特定的预处理指令。


您可以编写一个中介界面来提供诸如 LoadLib() LoadSym()等功能,它调用aproppriate本机API。换句话说,当编译Windows LoadLib()将使用 LoadLibraryA(),而在Linux上它将使用 dlopen()



这将让您从主代码中隐藏代码的这个丑陋的部分,使其保持更可读。


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.

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.