Reputation: 1
I'm following along with a video tut on SDL. When I do exactly as he has done, by putting an SDL_Texture* in the Game.h and use the object in Game.cpp, I get an error about multiple definitions. Yet, the video creator has no error of such. I can, however, copy and paste the SDL_Texture* to the .cpp and it work just fine.
Why is it that the same code will work on one, but not another machine, in Visual Studio
Game.cpp
#include "Game.h"
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 << "Successfully Initialized Subsystem . . . " << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (window)
{
std::cout << "Successfully Created Window . . . " << std::endl;
}
else
{
std::cout << "Something went wrong. Error: " << SDL_GetError() << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Successfully Created Renderer . . . " << std::endl;
}
else
{
std::cout << "Something went wrong. Error: " << SDL_GetError() << std::endl;
}
is_running = true;
}
else
{
is_running = false;
}
SDL_Surface* surf_temp = IMG_Load("assets/player.png");
tex_player = SDL_CreateTextureFromSurface(renderer, surf_temp);
SDL_FreeSurface(surf_temp);
}
void Game::event_handle()
{
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
is_running = false;
}
}
void Game::update()
{
cnt++;
std::cout << cnt << std::endl;
}
void Game::render()
{
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, tex_player, NULL, NULL);
SDL_RenderPresent(renderer);
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Game cleaned . . . " << std::endl;
}
Game.h
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <stdio.h>
#include <string>
SDL_Texture* tex_player;
class Game
{
public:
Game();
~Game();
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
void event_handle();
void update();
void render();
void clean();
bool running() { return is_running; }
private:
int cnt;
bool is_running;
SDL_Window *window;
SDL_Renderer *renderer;
};
main.cpp
#include "Game.h"
Game *game = nullptr;
int main(int argc, char *argv[])
{
game = new Game();
game->init("Yeah", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running())
{
game->event_handle();
game->update();
game->render();
}
game->clean();
return 0;
}
Error:
1>------ Build started: Project: SDL_Template, Configuration: Debug Win32 ------
1>main.cpp
1>LINK : C:\Users\Onion\documents\visual studio 2017\Projects\SDL_Template\Debug\SDL_Template.exe not found or not built by the last incremental link; performing full link
1>main.obj : error LNK2005: "struct SDL_Texture * tex_player" (?tex_player@@3PAUSDL_Texture@@A) already defined in Game.obj
1>C:\Users\Onion\documents\visual studio 2017\Projects\SDL_Template\Debug\SDL_Template.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Done building project "SDL_Template.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Upvotes: 0
Views: 122
Reputation:
Think of #include
as "please copy-paste the content of this file here."
Since you #include
Game.h in both main.cpp and Game.cpp, the line SDL_Texture* tex_player;
which is a global variable definition (as opposed to declaration) appears in both files, which confuses the linker.
The immediate fix is to declare the variable in game.h:
extern SDL_Texture* tex_player;
and define it in game.cpp
SDL_Texture* tex_player;
The better fix would be to make tex_player
a member variable of Game
(or some other class).
Upvotes: 1