jonnysoe / dltest

A quick start guide to creating a UNIX-style dynamic library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UNIX-style dynamic library example

A quick start guide to creating a UNIX-style dynamic library that calls functions from main program like a platform API.

Getting started

Requirements

Optional:

Usage

Demo

Either hit F5 in vscode after configuring a launch target in CMake Tools, or issue the following commands in the project directory to try out the programs:

cmake -S . -B build && cmake --build build
bin\dltest

Mechanism

Unlike UNIX which only requires the symbols to be available before calling them like a platform API, the approach in Windows is hacky and restricted to the main program it explicitly links against - so the plugins cannot be used on different main programs.

The main program will produce an import library (.lib) file by using CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS, it will set a module-definition (.def) file output under the hood which will be used to generate the .lib file for the plugin to link against.

While this demonstrates that it is possible to perform this UNIX-style library calling main program functions like a platform API, the proper way to share functions for library and main program is to move common code into another shared library where both will link against.

Do note that in Windows, it can still be compiled properly if the main program links against the common code as object/static library while the shared library links against the common code shared library, they will not share variable space, which will be different from expectations.

About

A quick start guide to creating a UNIX-style dynamic library

License:MIT License


Languages

Language:CMake 80.1%Language:C++ 12.3%Language:C 7.6%