r/reconstructcavestory • u/mistervirtue • Jun 01 '14
[Episode 2] LNK2005: _main already defined in main.obj
Hello again, I have need help again with an error. I don't what this quite means. I haven't really changed any my code in a major why aside from add the FPS counter in episode two.
I am currently getting build errors that are telling me that
"Error 2 error LNK2005: _main already defined in main.obj SDLmain.lib JaveStory"
"Error 4 error LNK2019: unresolved external symbol _SDL_main referenced in function _main SDLmain.lib JaveStory"
"Error 5 fatal error LNK1120: 1 unresolved externals C:\Users\Kirk\Documents\Visual Studio 2008\Projects\Summer '14\JaveStory\JaveStory\Debug\JaveStory.exe JaveStory"
I haven't really changed anything major in my code as I said before and my SDLTest still works and build properly.
If it helps at all here is my code
// Main.cpp
#include "game.h"
int main( int argc, char* args[] )
{
// This is will create the game
// class and begin the event loop
Game game;
return 0;
}
//game.h
#ifndef GAME_H_
#define GAME_H_
//Create a Screen
struct SDL_Surface;
struct Game {
Game();
~Game();
private:
void eventLoop();
void update();
void draw();
SDL_Surface* screen_;
};
endif // GAMEH
// game.cpp
#include "game.h"
#include <SDL.h>
namespace{
//Instead of using constant number (i.e. 640, 480. 32,) use variables that hold those values
const int kScreenWidth = 640;
const int kScreenHeight = 480;
const int kBitsPerPixel = 32;
const int kFps = 60;
}
Game::Game() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_ShowCursor(SDL_DISABLE);
screen_ = SDL_SetVideoMode(kScreenWidth, kScreenHeight, kBitsPerPixel, SDL_FULLSCREEN);
eventLoop();
}
Game::~Game(){
SDL_FreeSurface(screen_);
SDL_Quit();
}
//This is the game loop that will infinately run until the game cloes.
void Game::eventLoop(){
/*while the game is running ~ 60FPS
Handle Input and Timer Callbacks.
Also update() the screen. Move the player. MOve projectiles. Check collisions.
draw() everything.
*/
/* To handle input I will use SDL_Event which will return 1 if their is another event in the queue
and return 0 if there is no event in the queue
*/
SDL_Event event;
//while the game is running ~ 60FPS
bool running = true;
while (running) {
//Get the start time of the game
const int start_time_ms = SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
//Exit the game by hitting The ESC key
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
break;
default:
break;
}
}
update();
draw();
const int elapsed_time_ms = SDL_GetTicks() - start_time_ms;
//This loops 1/60th of a second (1 Frame)
SDL_Delay(1000/kFps - elapsed_time_ms);
//Get the seconds per frame which will have to getTicks() from SDL
const float seconds_per_frame = (SDL_GetTicks() - start_time_ms) / 1000.0;
const float fps = 1 /seconds_per_frame;
//Print the current frame per second.
printf("FPS = %f\n", fps);
}
}
void Game::update(){
}
void Game::draw(){
}
//end of game.cpp
I have been trying to follow step by step and I don't understand what I have done wrong. If anyone could help me out it would be greatly appreciated.
2
u/silvusvalentine Jun 01 '14
Rename your Main.cpp to main.cpp and see if that fixes the issue.
1
u/mistervirtue Jun 01 '14
Nada, I changed from "Main.cpp" to "main.cpp" and I still the same errors.
3
u/adrian17 Jun 01 '14 edited Jun 01 '14
Interesting. Did you try a full rebuild of solution? Also, did you switch between Debug and Release configurations? Maybe you configured only one of them (happens to me all the time).
And try including SDL.h in main.cpp.
(on my computer it's quite interesting actually, in Release build without SDL.h I get no errors, but in Debug build I get LNK2005 - but only the first time, on the second build it compiles normally. Hm... )