简体   繁体   中英

I get an “unresolved external symbol” error, but it can access them in visual studio LNK2019/LNK 1120

So I'm just trying to set up a game loop and window, but it won't even start with a blank screen. When I run it, it gives me an error every time I try to use any function from SDL. I have the.dll in the same folder as the project files and in the Debug folder. I'm pretty sure I have them linked in the project properties, too, but I don't know. When I'm in visual studio, I can hover over the functions and it has the declarations of them (I think)

SDL2\include is in VC++ Directories -> Include Directories

SDL2\lib\x64 is in VC++ Directories -> Library Directories

SDL2\include is also in C/C++ -> General -> Additional Include Directories

SDL2\lib\x64 is also in Linker -> General -> Additional Library Directories

SDL.lib and SDLmain.lib are both in Linker -> Input -> Additional Dependencies

edit: forgot to show project properties

This is Game.hpp

#include "SDL.h"
#include <iostream>

class Game 
{

public:
    Game();
    ~Game();

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

    bool running() { return isRunning; }

private:
    int cnt = 0;
    bool isRunning;
    SDL_Window *window;
    SDL_Renderer *renderer;
};

This is Game.cpp

#include "Game.hpp"

Game::Game()
{}
Game::~Game()
{}

void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{

    int flags = 0;
    if (fullscreen) 
    { 
        flags = SDL_WINDOW_FULLSCREEN;
    }

    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        std::cout << "Subsystems initialized" << std::endl;

        window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
        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;
        }

        isRunning = true;
    } else {
        isRunning = false;
    }
}

void Game::handleEvents()
{
    SDL_Event event;
    SDL_PollEvent(&event);
    switch (event.type)
    {
        case SDL_QUIT:
            isRunning = false;
            break;
        default:
            break;
    }
}

void Game::update()
{
    cnt++;
    std::cout << cnt << std::endl;
}

void Game::render()
{
    SDL_RenderClear(renderer);
    //add stuff to render

    SDL_RenderPresent(renderer);
}

void Game::clean()
{
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
    std::cout << "Game cleaned" << std::endl;
}

And this is main.cpp

#include "Game.hpp"

Game *game = nullptr;

int main(int argc, char *argv[]) {

    game = new Game();

    game->init("Maxgame", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

    while (game->running()) {

        game->handleEvents();
        game->update();
        game->render();

    }

    game->clean();

    return 0;
}

And these are the errors I'm getting.

You could set Linker -> Advanced -> Target Machine -> MachineX64 in Properties . 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM