SDL + C ++错误:LNK2019未解析的外部symbol_SDL_main referencedin函数

时间:2014-12-25 18:27:11

标签: sdl sdl-2 lnk2019

我谷歌这个问题,至少看过20个关于此问题的线程并尝试了不同的解决方案,但都没有解决。这个代码在Visual Studio 2013 Express的SDL 2.0.3中。这是我的代码:

#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>

class MGGMain{

int player_x;
int player_y;
int player_w = 50;
int player_h = 50;

SDL_Rect player_rect;
SDL_Texture* player_text;

SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event* mainEvent;

bool quit;

void Init()
{

}

void LoadAssets(SDL_Renderer* renderer)
{
    //IMG_LoadTexture(renderer, <path>);
    player_text = IMG_LoadTexture(renderer, "images/dev/dev_player");
}

void Update()
{
    int player_x;
    int player_y;
    SDL_Rect player_rect = CreateRect(player_h, player_w, player_x, player_y);

    if (mainEvent->type == SDL_QUIT)
    {
        quit = true;
    }
}

void Draw(SDL_Renderer* renderer)
{
    //SDL_RenderCopy(renderer, <texture>, <rect>)
    SDL_RenderCopy(renderer, player_text, &player_rect, &player_rect);
}

SDL_Rect CreateRect(int h, int w, int x, int y)
{
    SDL_Rect rect;
    rect.x = x;
    rect.y = y;
    rect.h = h;
    rect.w = w;
    return rect;
}

int main(int argc, char* argv[])
{
    std::cout << "Initialyzing SDL..." << std::endl;
    SDL_Init(SDL_INIT_EVERYTHING);
    std::cout << "All initialyzed, creating window and event system..." << std::endl;
    window = SDL_CreateWindow("MGG", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1200, 700, SDL_WINDOW_SHOWN);
    std::cout << "All succeded!" << std::endl;
    quit = false;
    if (window = NULL){
        std::cout << "SDL Error: Window couldn't be created!" << std::endl;
        return 0;
    }
    std::cout << "Creating renderer..." << std::endl;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    std::cout << "Renderer creation success, beginning main loop." << std::endl;
    mainEvent = new SDL_Event();
    LoadAssets(renderer);
    Init();
    while (!quit)
    {
        SDL_PollEvent(mainEvent);
        Update();
        SDL_RenderClear(renderer);
        Draw(renderer);
        SDL_RenderPresent(renderer);
    }
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    delete mainEvent;

    return 0;
}



};

1 个答案:

答案 0 :(得分:0)

您的程序没有main()函数。

代码中的main函数位于类MGGMain中,因此它变为MGGMain :: main()。它必须简单地是main(),在最高范围内,即在任何类之外,或者编译器将找不到它。

SDL错误输出是因为它覆盖了SDL程序使用的主要进行一些初始化然后调用真正的main(),但由于没有main(),它会抛出未解析的外部符号错误。