diff options
| author | Pali Rohár <pali.rohar@gmail.com> | 2024-11-21 01:52:34 +0100 |
|---|---|---|
| committer | Silvio Traversaro <silvio.traversaro@gbionics.ai> | 2026-05-20 22:36:45 +0200 |
| commit | 039e61578e868f229e30d4f4c25f9d6c3a4755ce (patch) | |
| tree | eee2855e59450763046121cb1f166d52c5266cee | |
| parent | 77152633a5d3d151107ce9a7b876747bce83c8d3 (diff) | |
| download | dlfcn-win32-fixpatches.tar.gz dlfcn-win32-fixpatches.tar.bz2 dlfcn-win32-fixpatches.zip | |
For non-debug DLL builds avoid linking to CRT library as it is not neededfixpatches
Now when dlfcn.c does not call any function from CRT library, it is not
needed to link dlfcn library with some specific CRT library (e.g.
crtdll.dll, msvcrt.dll, msvcr120.dll or api-ms-win-crt-*.dll) and therefore
bound the dlfcn DLL library to specific Visual C++ runtime version.
This makes libdl.dll DLL library CRT-neutral and therefore can be used or
linked into any EXE application using any CRT library of any version,
without any Visual C++ runtime version conflict.
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | src/dlfcn.c | 11 |
3 files changed, 20 insertions, 1 deletions
| @@ -32,7 +32,7 @@ libdl.a: $(SOURCES) | |||
| 32 | $(RANLIB) $@ | 32 | $(RANLIB) $@ |
| 33 | 33 | ||
| 34 | libdl.dll: $(SOURCES) | 34 | libdl.dll: $(SOURCES) |
| 35 | $(CC) $(CFLAGS) $(SHFLAGS) -DDLFCN_WIN32_SHARED -shared -o $@ $^ | 35 | $(CC) $(CFLAGS) $(SHFLAGS) -DDLFCN_WIN32_SHARED -DDLFCN_WIN32_SHARED_ENTRYPOINT -D_DllMainCRTStartup=DllMainCRTStartup -nostartfiles -nostdlib -shared -o $@ $^ -lkernel32 |
| 36 | 36 | ||
| 37 | libdl.lib: libdl.dll | 37 | libdl.lib: libdl.dll |
| 38 | $(LIBCMD) /machine:i386 /def:libdl.def | 38 | $(LIBCMD) /machine:i386 /def:libdl.def |
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9066e65..2fbf0d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt | |||
| @@ -15,6 +15,14 @@ set_target_properties(dl PROPERTIES DEFINE_SYMBOL "") | |||
| 15 | 15 | ||
| 16 | # set shared mode for compiling library and propagate mode to cmake clients | 16 | # set shared mode for compiling library and propagate mode to cmake clients |
| 17 | if (BUILD_SHARED_LIBS) | 17 | if (BUILD_SHARED_LIBS) |
| 18 | # for non-debug shared mode, avoid linking to CRT library as it is not needed | ||
| 19 | if (MSVC) | ||
| 20 | set_property(TARGET dl APPEND PROPERTY COMPILE_OPTIONS $<$<NOT:$<CONFIG:Debug>>:/Zl /GS->) | ||
| 21 | else (MSVC) | ||
| 22 | set_property(TARGET dl APPEND PROPERTY COMPILE_DEFINITIONS $<$<NOT:$<CONFIG:Debug>>:_DllMainCRTStartup=DllMainCRTStartup>) | ||
| 23 | set_property(TARGET dl APPEND PROPERTY LINK_OPTIONS $<$<NOT:$<CONFIG:Debug>>:-nostartfiles -nostdlib -lkernel32>) | ||
| 24 | endif (MSVC) | ||
| 25 | set_property(TARGET dl APPEND PROPERTY COMPILE_DEFINITIONS $<$<NOT:$<CONFIG:Debug>>:DLFCN_WIN32_SHARED_ENTRYPOINT>) | ||
| 18 | target_compile_definitions(dl PUBLIC DLFCN_WIN32_SHARED) | 26 | target_compile_definitions(dl PUBLIC DLFCN_WIN32_SHARED) |
| 19 | endif (BUILD_SHARED_LIBS) | 27 | endif (BUILD_SHARED_LIBS) |
| 20 | 28 | ||
diff --git a/src/dlfcn.c b/src/dlfcn.c index c128579..a9c9d8b 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c | |||
| @@ -955,4 +955,15 @@ BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) | |||
| 955 | (void) lpvReserved; | 955 | (void) lpvReserved; |
| 956 | return TRUE; | 956 | return TRUE; |
| 957 | } | 957 | } |
| 958 | |||
| 959 | /* When requested defines DLL entry point which avoids using CRT library */ | ||
| 960 | #ifdef DLFCN_WIN32_SHARED_ENTRYPOINT | ||
| 961 | #ifdef __cplusplus | ||
| 962 | extern "C" | ||
| 963 | #endif | ||
| 964 | BOOL WINAPI _DllMainCRTStartup( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) | ||
| 965 | { | ||
| 966 | return DllMain( hinstDLL, fdwReason, lpvReserved ); | ||
| 967 | } | ||
| 968 | #endif | ||
| 958 | #endif | 969 | #endif |
