Sokol: Add DLL build option

Created on 3 Jun 2019  路  6Comments  路  Source: floooh/sokol

Most helpful comment

Here's what it would look like:

https://github.com/floooh/sokol/blob/7c88e7736e813479595489bb6acaa373d25fd1f8/sokol_gfx.h#L489-L497

You'd just define SOKOL_DLL both when building and using the DLL. When building the DLL (SOKOL_IMPL is also defined) it will use __declspec(dllexport), otherwise __declspec(dllimport).

I need to do this for all headers, and actually test on Windows before merging, might take a few days.

All 6 comments

Hah, good timing :) Just wanted to add a ticket myself.

Here's what it would look like:

https://github.com/floooh/sokol/blob/7c88e7736e813479595489bb6acaa373d25fd1f8/sokol_gfx.h#L489-L497

You'd just define SOKOL_DLL both when building and using the DLL. When building the DLL (SOKOL_IMPL is also defined) it will use __declspec(dllexport), otherwise __declspec(dllimport).

I need to do this for all headers, and actually test on Windows before merging, might take a few days.

Sounds just right!

FYI, I did something similar under Windows 10 x64 (MSVC 2017), and it works like a charm for sokol-gfx!

New sample code to test building and using sokol as DLL (currently Windows only):

sokol-dll.c implementation file:

https://github.com/floooh/sokol-samples/blob/dll-defines/libs/sokol/sokol-dll.c

fips/cmake definition to create the DLL:

https://github.com/floooh/sokol-samples/blob/12e22867129b2c8a59ee7e85e5800b72fc56bcaf/libs/sokol/CMakeLists.txt#L59-L66

And an executable using sokol as DLL (note the SOKOL_DLL define, that's the only difference):

https://github.com/floooh/sokol-samples/blob/dll-defines/sapp/noentry-dll-sapp.c

fips/cmake definitions:

https://github.com/floooh/sokol-samples/blob/12e22867129b2c8a59ee7e85e5800b72fc56bcaf/sapp/CMakeLists.txt#L353-L361

...and the imports/exports via dumpbin, just to make sure it actually works :)

> dumpbin /exports sokol-dll.dll:
sokol-dll-exports

The sokol-dll.dll depends on kernel32.dll, user32.dll, ole32.dll and d3d11.dll (with d3dcompiler_47.dll loaded on demand).

> dumpbin /imports noentry-dll-sapp.exe
sokol-dll-imports

The exe only depends on kernel32.dll and sokol-dll.dll.

Currently this is still all in a branch, and only the "main headers" are working. Next I'll update the headers in the util directory too, and merge to master.

Ok, everything done and merged. It may also make sense to add function pointer typedefs, for situations where the DLL is loaded manually via dlopen or LoadLibrary, but I'll wait with this until anybody actually needs it :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DomDumont picture DomDumont  路  6Comments

siavashserver picture siavashserver  路  6Comments

floooh picture floooh  路  3Comments

sherjilozair picture sherjilozair  路  7Comments

PeterHiber picture PeterHiber  路  5Comments