Compiling error: function already has a body

Hi!

I’m making a program on Visual Studio 2017 and I keep getting error code C2084
function 'int SDL_main(int,char *[])' already has a body
which as per Microsoft’s documentation says that the function has already been declared previously.
However, I have only declared the main function once on the entire program so I’m not sure what might be causing it. Any ideas what it might be?
The main.cpp I currently have is this and it’s the only file with a main function:

#define SDL_MAIN_HANDLED
#include <stdio.h>
#include <fstream>
#include <stdint.h>
#include <SDL.h>
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
#include <istream>
#include <SDL_image.h>
#include <scrnInfo.h>
#include <runtime.h>
#include <renderFunctions.h>
#include <SDL_ttf.h>


using namespace std;



int main(int, char**)
{
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
	{
		logSDLError(cout, "SDL_Init");
		return 1;
	}
	//Loading config file below
	//scrnInfo info = scrnInfo();
	//info.readConfig("config.ini");
	SDL_Window *win = SDL_CreateWindow("Want some succ?", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN);
	if (win == nullptr)
	{
		logSDLError(cout, "SDL_CreateWindow");
		SDL_Quit();
		return 1;
	}

	Config info = Config(win);
	info.readConfig("config.ini", win);

	SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
	SDL_Event event;
	bool quit = false;
	bool displaySplash = true;
	bool ngplus = info.getNgPlus();

	SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (ren == nullptr)
	{
		logSDLError(cout, "SDL_CreateRenderer");
		SDL_Quit();
		return 1;
	}

	/*SDL_Texture *splash = loadTexture("Resources/BG/splash_placeholder.png", ren);
	if (splash == nullptr)
	{
		SDL_DestroyTexture(splash);
		SDL_DestroyRenderer(ren);
		SDL_DestroyWindow(win);
		logSDLError(cout, "loadTexture");
		return 1;
	}
	*/
	while (!quit)
	{
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case (SDL_QUIT):
			{
				quit = true;
				return 0;
			}
			}
			switch (event.key.keysym.sym)
			{
			case (SDLK_LALT | SDLK_F4):
			{
				quit = true;
				return 0;

			}
			case (SDLK_LALT | SDLK_RETURN):
			{
				bool status = info.getFullscreenStatus();
				if (!status)
				{
					SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
				}
				else
				{
					SDL_SetWindowFullscreen(win, 0);
				}
			}
			}

		}
		if (displaySplash == true)
		{
			FadingSplashScreen(2000, ren, info , 255, 255, 255, 255);
			displaySplash = false;
		}
		menuLoop(ngplus, win, ren, quit, &info);
	}
}

Thanks for your attention!

Hi, I am just a Linux user. Have you checked http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvsnet2010u/index.php
?

Thanks for your reply,
I originally followed Will Usher’s tutorial, but everything is configured just as in the one you sent me, and it was working up until I tried compiling it today…

Alright I checked:
http://www.willusher.io/sdl2%20tutorials/2013/08/15/lesson-0-visual-studio
but is
#define SDL_MAIN_HANDLED
in your code needed?

If you try to compile:

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

int main(int, char**){
	if (SDL_Init(SDL_INIT_VIDEO) != 0){
		std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
		return 1;
	}
	SDL_Quit();
	return 0;
}

Does it work?

Just tried that, still giving me the same error

@nicolau,

A few things that could help you:

  1. Start with something simpler, like it was suggested by @Acry
  2. Start adding a libraries as you need. There are too many libraries in your code and I wonder if you need them all…
  3. When using the pragma #define add also ifndef (before) and endif (after the code). Also, they are usually used in the header files
  4. The error is that a function has already been defined. It doesn’t mean it is your main function. Could be another function in a header file out there…
1 Like

That would have been my next shot, if he pulls another main() with the headers. But think I was reading the same code compiled before; so I thought it’s a good idea to let VS-Users deal with the issue.

1 Like

@tupan thanks, the ifndef worked, but yeah my code isn’t the cleanest it could be so I guess next on my priority list will be cleaning it up

The error message should have also included a file name and a line number where the error occurred and where the previous definition was. This helps you to pinpoint the position of the bug. I’m guessing it is in one of your other header files?

I don’t think SDL is responsible, it does have include guards.