我在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我知道链接到一个动态链接库是没有意义的,但是我怎么能欺骗CMake不去照顾RegisterPowerSettingNotification呢?
发布于 2016-05-11 22:09:30
从Windows Vista开始,可以使用RegisterPowerSettingNotification。在Linux下使用MinGW编译,默认的WINVER是0x502 (Windows Server2003):/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
#endifhttps://stackoverflow.com/questions/37139853
复制相似问题