MinGW,g ++,winapi,winsdk,迷茫中 [英] MinGW, g++, winapi, winsdk, lost in confusion

查看:127
本文介绍了MinGW,g ++,winapi,winsdk,迷茫中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用g ++(mingw)调用GetPhysicalCusrorPos()进行编译,结果发现我的SDK已过时.因此我去下载了最新的w32api的最新的MinGW,但是标头中仍然缺少此功能...因此我开始在以下问题上进行搜索,只是越来越迷路了:

I was trying to call GetPhysicalCusrorPos() compiling with g++ (mingw), and it turned out that I my sdk is outdated. So I went to download the last MinGW with latest w32api, but this function is still missing from the headers... So I started googling around about the following questions, only to get more and more lost :

_当您想使用Windows API但要使用MinGW时,w32api是实现它的好方法吗?

_ When you want to work with the windows API but with MinGW, is w32api the good and only way to reach it ?

_ Windows API,Windows SDK,w32api,MinGW和w32api之间的确切关系是什么?

_ What is exactly the relationship between the Windows API, the windows SDK, w32api, MinGW and the w32api ???

_ gnu工具(MinGW可以使用gcc,g ++等)与VisualC/WinSDK(cl.exe)一样吗?

_ Can the gnu tools (MinGW so gcc, g++ etc) do the same as VisualC/WinSDK (cl.exe) ???

请不要以为我没有寻找答案,我当然可以,但是我发现的所有不同答案使我迷失的程度超过了他们的帮助. 如果有人能澄清所有这些,对于像我这样的所有迷路初学者来说将是非常不错的.

Please don't suppose that I didn't search for answers, I surely did but all the different answers I found lost me more than they helped me. If someone could clarify all that would be very nice for all the lost beginners like me.

非常感谢!

推荐答案

据我了解,将MS SDK与mingw编译器一起使用是不切实际的.您必须使用mingw特定的SDK.甚至最新的mingw版本似乎也不支持GetPhysicalCursorPos.

As I understand it, it is not practical to use the MS SDK with the mingw compiler. You have to use a mingw specific SDK. And even the most up-to-date mingw releases appear not to support GetPhysicalCursorPos.

由于您仅需要此功能,或者可能仅需要此功能以及其他几个功能,因此可以很容易地自己提供SDK的缺失部分.例如:

Since you only need this one function, or perhaps this one and a handful of others, it is easy enough to supply the missing parts of the SDK yourself. For example:

#define WINVER 0x0600
#include <windows.h>
#include <iostream>

extern "C"
{
    __declspec(dllimport) BOOL WINAPI GetPhysicalCursorPos(LPPOINT lpPoint);
}

int main()
{
    POINT pt;
    if (GetPhysicalCursorPos(&pt))
    {
        std::cout << pt.x << ", " << pt.y << std::endl;
    }
}

在这里,我们包含了我们希望调用的API的声明.编译此文件时,会出现以下错误:

Here we include a declaration of the API we wish to call. When you compile this you get the following error:


>g++ main.cpp -o main.exe
>C:\Users\xxx\AppData\Local\Temp\ccCCZZEk.o:main.cpp:(.text+0x1d): undefined 
reference to `__imp_GetPhysicalCursorPos'
collect2.exe: error: ld returned 1 exit status

因此,我们需要找到一种使链接程序能够解析符号的方法.从理论上讲,您可以要求链接程序ld直接链接到DLL.但是我还无法使它正常工作.相反,您可以使用更费力的方法.

So we need to find a way for the linker to be able to resolve the symbol. In theory you can ask the linker, ld, to link to DLLs directly. But I have not been able to get that to work. Instead you can use a somewhat more laborious approach.

制作一个.def文件来定义该函数所在的user32库的导出:

Make a .def file to define the exports of the user32 library where this function lives:


LIBRARY user32.dll
EXPORTS
    GetPhysicalCursorPos = __imp_GetPhysicalCursorPos

将其保存到名为myuser32.def的文件中.现在,这不是user32导出的所有功能,但是我们只想要mingw随附的导入库中缺少的功能.

Save that to a file named myuser32.def. Now, that's not all the functions that user32 exports, but we only want the ones that are missing from the import library that comes with mingw.

现在使用dlltool创建导入库:

Now use dlltool to make an import library:


>dlltool -d myuser32.def -l myuser32.a

然后通过传递库进行编译:

And then compile passing the import library:


>g++ main.cpp myuser32.a -o main.exe

这次成功了.当我们运行程序时:

Success this time. When we run the program:


>main
1302, 670

这篇关于MinGW,g ++,winapi,winsdk,迷茫中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆