2016-05-10 73 views
0

我在Linux下使用mingw,我試圖編譯Windows。我使用的CMake和ouptut應該是一個.exe文件。 在我的程序中,我使用了user32.dll/user32.lib中的WinAPI調用(RegisterPowerSettingNotification)。我想讓我的.exe獨立於user32.dll版本(我的.exe應該在Windows8/8.1/10上運行)。當在Linux下編譯時,「鏈接」到cmake中的user32.dll(交叉)

我的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?

回答

0

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