Chuyu-Team / YY-Thunks

Fix DecodePointer, EncodePointer,RegDeleteKeyEx etc. APIs not found in Windows XP RTM.
MIT License
567 stars 103 forks source link

分享一种可以在 CMake+MSVC 中使用 yy-thinks 的方法 | Share an approach to link yy-thunks in CMake+MSVC #102

Open BH2WFR opened 3 months ago

BH2WFR commented 3 months ago

以下代码可以让项目正常链接到 yy-thunks 的 .obj 库: the code below can let the CMake project correctly link yy-thunks .obj libraries.

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" CACHE STRING "" FORCE)      # /MTd in Debug, /MT in Release
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:DebugDLL>" CACHE STRING "" FORCE) # /MDd in Debug, /MT in Release
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" CACHE STRING "" FORCE) # /MDd in Debug, /MD in Release (默认)

#* yy-thunks (MSVC only)
set(YY_THUNKS_DIR "D:/3rdlibs/YY-Thunks")   #* yy-thunks 根目录
set(YY_THUNKS_WIN_VERSION "WinXP")  #* 用于匹配文件名, 可用值: `WinXP` `Vista` `Win7` `Win8` `Win10.0.10240` `Win10.0.19041`, 用于匹配文件名
set(YY_THUNKS_WIN_VER_STR "5.1")    #* Windows 子系统版本, 5.1:XP, 5.2:2003, 6.0:Vista, 6.1:Win7, 6.2:Win8, 6.3:Win8.1, 10.0:Win10/11, 留空则默认
set(YY_THUNKS_ARCH  "x64")
if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 位
    set(YY_THUNKS_ARCH "x86")
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") #* MSVC, 抢在系统库 lib 前挂入 yy-thunks 的 obj 文件
    set(YY_THUNKS_OBJ_NAME "${YY_THUNKS_DIR}/objs/${YY_THUNKS_ARCH}/YY_Thunks_for_${YY_THUNKS_WIN_VERSION}.obj") # 查找 obj 文件
    set(CMAKE_CXX_STANDARD_LIBRARIES "\"${YY_THUNKS_OBJ_NAME}\" ${CMAKE_CXX_STANDARD_LIBRARIES}")
    set(CMAKE_C_STANDARD_LIBRARIES   "\"${YY_THUNKS_OBJ_NAME}\" ${CMAKE_C_STANDARD_LIBRARIES}")
    if(YY_THUNKS_WIN_VER_STR)
        add_link_options("-SUBSYSTEM:$<IF:$<BOOL:${CMAKE_WIN32_EXECUTABLE}>,WINDOWS,CONSOLE>,${YY_THUNKS_WIN_VER_STR}")
    endif()
else()
    message(WARNING "yy-thunks obj files only support MSVC linker, this operation will be ignored...")
endif()

. .

使用方法: 在上述代码中分别设置 YY_THUNKS_DIR YY_THUNKS_WIN_VERSION YY_THUNKS_WIN_VER_STR 三个变量, 指定路径和目标系统版本 . Usage: set variables YY_THUNKS_DIR YY_THUNKS_WIN_VERSION YY_THUNKS_WIN_VER_STR to your path and target system version

. . 如需运行在 Windows XP 中, 需要在所有 DLL 项目中额外调用: If you need to run in Windows XP, the code below is needed in each DLL target. target_link_options(${PROJECT_NAME} PRIVATE "/ENTRY:DllMainCRTStartupForYY_Thunks")

mingkuang-Chuyu commented 3 months ago

请问是否可以考虑提交PR,给yY-Thunks提供官方cmake支持。

BH2WFR commented 3 months ago

请问是否可以考虑提交PR,给yY-Thunks提供官方cmake支持。

我交了一个PR,把这个写到 Readme.md 里面了