我有一个小项目,需要在 C++ 中使用 Win32 API。
我缺乏有关此类应用程序的构建步骤的一些知识,因此我想在不使用 Visual Studio 的情况下完成它,以教自己事物在幕后是如何工作的。
为此,我获得了 VS Code、MinGW、Make for Windows 和 Win32 API。
在弄清楚 MinGW 和 Make 工具如何工作一段时间后,我正在尝试了解库和依赖项。
这样做,我想编译这个真正简单的代码:
/*
header file coming from the win32 api,
giving access to the Windows Wallpaper, which is what I want to mess with
*/
#include "ShObjIdl_core.h"
int main() {
return 0;
}
在我的 Makefile 中,我包含了两个文件夹,其中存储了我想要的 win32 头文件:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0
\um
\Shared
链接似乎成功,但问题来了:编译器向我抛出来自许多不同文件的大量不同错误。
这里有一些例子:
- error: #error "No Target Architecture"`
- warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
#pragma warning(disable:4214) // bit field types other than int
- error: cast from 'const void*' to 'long unsigned int' loses precision [-fpermissive]
return((void *) (ULONG_PTR) (unsigned long) p);
- error: 'PCONTEXT' was not declared in this scope;
我已经看到here我应该
#include "windows.h"
而不是我的特定文件,但这并不能解决问题,我仍然收到一堆错误。
我觉得我错过了有关使用 API 或一般依赖项的重要信息,但我在网络上找不到任何内容来帮助我理解它应该如何工作。
MinGW 带有自己的 Win32 API 标头和库。
所以不要将其与 Microsoft 的标头混合。
只需使用
#include <windows.h>
就足够了。
MinGW GCC 编译器会知道在哪里找到它。
对于链接,您将添加必要的 -l
标志,对于这些标志,MinGW GCC 也会知道在哪里查找。