在Linux下编译时,在cmake中“链接”到user32.dll(交叉)

时间:2016-05-10 13:22:17

标签: dll cmake mingw

我在Linux下使用mingw,我正在尝试为Windows编译。我正在使用CMake,而ouptut应该是.exe文件。 在我的程序中,我正在使用在user32.dll / user32.lib中找到的WinAPI调用(RegisterPowerSettingNotification)。我希望我的.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?

1 个答案:

答案 0 :(得分:0)

从Windows Vista启动RegisterPowerSettingNotification。在Linux下使用MinGW进行编译,默认情况下,WINVER为0x502(Windows Server 2003):/ usr / share / mingww -w64 / include / __mw,未定义RegisterPowerSettingNotification。

解决方案是在任何

之前添加
#include <windows.h>

WINVER和_WIN32_WINNT的定义。

#ifndef WINVER
#define WINVER 0x0600
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif