From 529de2d3d04a41f869dd4803404cd763b9e8f72c Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 21 Nov 2024 01:52:34 +0100 Subject: For non-debug DLL builds avoid linking to CRT library as it is not needed 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. --- src/CMakeLists.txt | 9 +++++++++ src/dlfcn.c | 11 +++++++++++ 2 files changed, 20 insertions(+) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9066e65..5dc21d3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,15 @@ set_target_properties(dl PROPERTIES DEFINE_SYMBOL "") # set shared mode for compiling library and propagate mode to cmake clients if (BUILD_SHARED_LIBS) + # for non-debug shared mode, avoid linking to CRT library as it is not needed + if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + if (MSVC) + set_property(TARGET dl APPEND_STRING PROPERTY COMPILE_FLAGS "/Zl") + else (MSVC) + set_property(TARGET dl APPEND_STRING PROPERTY COMPILE_FLAGS "-D_DllMainCRTStartup=DllMainCRTStartup") + set_property(TARGET dl APPEND_STRING PROPERTY LINK_FLAGS "-nostartfiles -nostdlib -lkernel32") + endif (MSVC) + endif (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") target_compile_definitions(dl PUBLIC DLFCN_WIN32_SHARED) endif (BUILD_SHARED_LIBS) diff --git a/src/dlfcn.c b/src/dlfcn.c index c128579..5ddf1a7 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c @@ -955,4 +955,15 @@ BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) (void) lpvReserved; return TRUE; } + +/* For non-debug DLL builds defines DLL entry point which avoids using CRT library */ +#if !defined( _DEBUG ) +#ifdef __cplusplus +extern "C" +#endif +BOOL WINAPI _DllMainCRTStartup( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) +{ + return DllMain( hinstDLL, fdwReason, lpvReserved ); +} +#endif #endif -- cgit v1.2.3-55-g6feb