将我自己制作的库从 Macintosh 移植到 Windows,看起来我遇到了 Windows 中的预定义符号:
[build] In file included from C:/msys64/mingw64/include/windows.h:72,
[build] from C:/Users/ken/Desktop/Drawbox/Library/Win/Drawbox.Win.cpp:31:
[build] C:/msys64/mingw64/include/winuser.h:3766:29: error: ambiguating new declaration of 'HICON__* GetCursor()'
[build] 3766 | WINUSERAPI HCURSOR WINAPI GetCursor(VOID);
[build] | ^~~~~~~~~
[build] In file included from C:/Users/ken/Desktop/Drawbox/Library/Win/Drawbox.Win.cpp:15:
[build] C:/Users/ken/Desktop/Drawbox/Library/Drawbox.h:559:5: note: old declaration 'int GetCursor()'
[build] 559 | int GetCursor();
[build] | ^~~~~~~~~
嗯,我真的不想要 Windows 定义的那个,我很乐意将它隐藏在我定义的那个中。所以我尝试了:
#define GetCursor() GetCursorX()
#include <windows.h>
#include <mmsystem.h>
#undef GetCursor
但是不,这会让编译器更加不高兴:
[ 6%] [32mBuilding CXX object CMakeFiles/TestProgram.dir/Library/Win/Drawbox.Win.cpp.obj[0m
[build] In file included from C:/msys64/mingw64/include/windows.h:72,
[build] from C:/Users/ken/Desktop/Drawbox/Library/Win/Drawbox.Win.cpp:32:
[build] C:/msys64/mingw64/include/winuser.h:3766:43: error: macro "GetCursor" passed 1 arguments, but takes just 0
[build] 3766 | WINUSERAPI HCURSOR WINAPI GetCursor(VOID);
[build] | ^
[build] C:/Users/ken/Desktop/Drawbox/Library/Win/Drawbox.Win.cpp:31: note: macro "GetCursor" defined here
[build] 31 | #define GetCursor() GetCursorX()
这是可行的,还是我只是坚持重命名我的函数以在 Windows 上工作?
答案:windows.h 中定义的许多函数实际上都是宏,它们解析为带有后缀的某些相关名称。因此,如果您尝试重新定义其宏,windows.h 会非常不高兴,但之后取消定义它们却可以正常工作:
#include <windows.h>
#include <mmsystem.h>
#undef GetCursor
工作起来很有魅力。