SDL2 和 vcpkg 遇到关于“invoke_main(void)”的链接器问题

问题描述 投票:0回答:1

我知道有些问题具有类似的问题,并且我已经检查了herehere,但这两个问题都没有提出似乎可以解决我的问题的解决方案。我的子系统设置为 Windows(如其他帖子中的建议)。我今天使用 Visual Studio 2022 并安装了 vcpkg 及其库。一些解决方案建议对 CMake 进行更改,但我正在使用 vcpkg 并且完全不确定如何实施这些解决方案。如果这很简单,我很抱歉;我是 SDL 的新手,正在关注 YouTube 系列来学习。这与我正在关注的系列的代码完全相同,但代码在video中正确编译。

我将包含我认为相关的代码。如果需要,请随时询问。

main.cpp

#include "Game.h"
int main()
{
    Game* game = new Game();
    game->init("Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

    while (game->running())
    {
        game->handle_events();
        game->update();
        game->render();
    }

    game->clean();
    delete game;
    return 0;
}

游戏.h

#pragma once

#include <SDL2/SDL.h>

class Game {
public:
    Game();
    ~Game();

    void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
    
    void handle_events();
    void update();
    void render();
    void clean();

    [[nodiscard]] bool running() const;

private:
    bool is_running_;
    SDL_Window* window_;
    SDL_Renderer* renderer_;
};

游戏.cpp

void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) 
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        std::cout << "Subsystems Initialized!" << std::endl;

        window_ = SDL_CreateWindow(title, xpos, ypos, width, height, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
        if (window_)
            std::cout << "Window created!" << std::endl;

        renderer_ = SDL_CreateRenderer(window_, -1, 0);
        if (renderer_)
        {
            SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);

            std::cout << "Renderer created" << std::endl;
        }

        is_running_ = true;
    }
    else
    {
        is_running_ = false;
    }       
}

尝试编译时,这是我的调试配置构建日志:

Build started at 12:30 AM...
1>------ Build started: Project: SDLGame, Configuration: Debug x64 ------
1>main.cpp
1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>G:\My Drive\Dev\C++\Examples\SDLGame\SDLGame\bin\Debug-x64\SDLGame.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "SDLGame.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 12:30 AM and took 01.942 seconds ==========

这是发布配置的构建日志:

Build started at 12:55 AM...
1>------ Build started: Project: SDLGame, Configuration: Release x64 ------
1>Game.cpp
1>main.cpp
1>MSVCRT.lib(exe_winmain.obj) : error LNK2001: unresolved external symbol WinMain
1>G:\My Drive\Dev\C++\Examples\SDLGame\SDLGame\bin\Release-x64\SDLGame.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "SDLGame.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 12:55 AM and took 03.084 seconds ==========
c++ visual-studio sdl linker-errors vcpkg
1个回答
0
投票
  1. 使用
    int main(int argc, char* argv[])
    代替
    int main()
  2. 添加lib依赖 调试:
    #pragma comment(lib, "sdl2maind")
    释放
    #pragma comment(lib, "sdl2main")

找到lib文件夹,添加额外的库目录,如debug:

vcpkg-master\installed\x64-windows\debug\lib\manual-link

© www.soinside.com 2019 - 2024. All rights reserved.