使用 g++ 的 C++ XInput 编译

C++ XInput Compilation using g++

#include <Windows.h>
#include <XInput.h>

#include <iostream>

using namespace std;

struct Controller{
    XINPUT_STATE state;
};

class Joypad
{
public:
    int getJoystickPort()
    {
        DWORD dwResult;

        for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
        {
            XINPUT_STATE state;
            ZeroMemory(&state, sizeof(XINPUT_STATE));   

            // Simply get the state of the controller from XInput.
            dwResult = XInputGetState(i, &state);

            if (dwResult == ERROR_SUCCESS)
            {
                return((int) i);
                cout << "Joystick Port: " << i << " is connnected." << endl;
                cout << "Button " << (((int)state.Gamepad.wButtons == XINPUT_GAMEPAD_A)) << " pressed." << endl;
                cout << "LX:  " << state.Gamepad.sThumbLX << " LY: " << state.Gamepad.sThumbLY << endl;
                cout << "RX:  " << state.Gamepad.sThumbRX << " RY: " << state.Gamepad.sThumbRY << endl;
            }
            else
            {
                cout << "Joystick at Port: " << i << " is disconnected." << endl;
            }
        }
        return -1;
    }
};



void joystickStates(){
    Joypad* joypad = new Joypad;
    while (true){
        system("cls");      
        joypad->getJoystickPort();
        Sleep(1000);
    }
}

int main(){
    joystickStates();
    return 0;
}

我收到 __in & __out 的未在此范围内声明的错误。

我使用了下面的语法 g++ Joypad.cpp -IC:\DirectSDK\Include -LC:\DirectSDK\Lib\x86 -lXinput -o Joypad

我也试过了 g++ Joypad.cpp -IC:\Windows SDK~um,shared, etc -LC:\Windows SDK\Lib -lXInput -o Joypad

有什么我遗漏的吗?我用 mingw x86_64

Directories included: Windows Kits 8.1 (um,shared,winrt) Microsoft DirectX SDK

Libraries included: XInput.lib - C:\Progra~2\Micros~4\Lib\x86

__in__out 是 Microsoft 特定的 SAL 注释。它们可以被 MS Visual Studio C++ 编译器理解,但不能被 GCC 编译器理解。

一种 "delete" 它们的方法是为自己制作一个头文件,比如 no_sal.h 和 每次你的 GCC 编译在 SAL 注释 __foo 上 barfs 时,添加:

#define __foo

no_sal.h。然后确保每个编译中首先包含 no_sal.h 通过传递 g++ 选项 -include /path/to/no_sal.h.

但是,我不希望这就是您尝试 使用 GCC 编译像 DirectX SDK 这样非常 "deeply Microsoft" 的代码。如果你 不想使用 Visual Studio 然后考虑切换你的 Windows GCC 从 MinGW(我猜)分发到 TDM GCC 64-bit, 它为 GCC 捆绑了 DirectX 头文件和库。