【问题标题】:"Link" to user32.dll in cmake when compile under Linux (cross)在 Linux 下编译时“链接”到 cmake 中的 user32.dll(交叉)
【发布时间】:2016-05-10 13:22:17
【问题描述】:

我在 Linux 下使用 mingw,我正在尝试为 Windows 编译。我正在使用 CMake,输出应该是 .exe 文件。 在我的程序中,我使用了一个 WinAPI 调用 (RegisterPowerSettingNotification),它位于 user32.dll/user32.lib 中。我想让我的 .exe 独立于 user32.dll 版本(我的 .exe 应该在 Windows8/8.1/10 上运行)。

我的 CmakeLists.txt:

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${USBHID_HEADER}
    )
#USER32 // Will find the user32.lib
find_library(USER32 user32)
list(APPEND USBHID_LIB_DEPS ${USER32})
find_path(USBHID_HEADER winuser.h)
list(APPEND INCLUDES ${USBHID_HEADER})

# add the executable
add_executable( myexe win_service.c resource.rc )
target_link_libraries( myexe LINK_PUBLIC ${USBHID_LIB_DEPS} )
set_target_properties( myexe PROPERTIES OUTPUT_NAME "MyExeService" )
install( TARGETS myexe DESTINATION bin)

我在编译时收到警告:

/.../win_service.c:182:27: warning: assignment makes pointer from integer without a cast [enabled by default]
lidcloseRegHandle = RegisterPowerSettingNotification(serviceStatusHandle, &GUID_LIDCLOSE_ACTION,...

在链接时:

Linking C executable myexeservice.exe
CMakeFiles/myexeservice.dir/objects.a(win_service.c.obj):win_service.c:(.text+0x393): undefined reference to `RegisterPowerSettingNotification'
collect2: error: ld returned 1 exit status

我知道链接到 DLL 是没有意义的,但我怎么能欺骗 CMake 使其不关注 RegisterPowerSettingNotification?

【问题讨论】:

    标签: dll cmake mingw


    【解决方案1】:

    RegisterPowerSettingNotification 从 Windows Vista 开始可用。 Linux下使用MinGW编译,WINVER默认为0x502(Windows Server 2003):/usr/share/mingw-w64/include/_mingw.h,RegisterPowerSettingNotification未定义。

    解决方案是在任何之前添加

    #include <windows.h>
    

    WINVER 和 _WIN32_WINNT 的定义。

    #ifndef WINVER
    #define WINVER 0x0600
    #endif
    
    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0600
    #endif
    

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 2021-09-07
      • 2015-07-21
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多