2

I'm attempting to compile my SDL project on my Windows 10 64bit machine. Unfortunately, when compiling using mingw32-make. My compile fails just before it's done linking throwing good old "undefined reference to `SDL_main'". I've made sure I use the mingw dev libraries "i686-w64-mingw32". (I also switch to "x86_64-w64-mingw32", but same results.) and #undef main after I include SDL.

Here is the console output after running mingw32-make...

g++ ./neo/engine/gamesys/EngineCore.cpp ./neo/engine/gamesys/Logger.cpp ./neo/engine/gamesys/EventDispatcher.cpp ./neo/engine/renderer/Renderer.cpp ./neo/engine/renderer/RendererSetup.cpp  -IC:\\mingw_dev_lib\\SDL2-2.0.4i686\\include -LC:\mingw_dev_lib\\SDL2-2.0.4i686\lib -w -Wl,-subsystem,windows -lmingw32  -lSDL2main -lSDL2 -lopengl32 -lglu32 -o pixelHell
C:\mingw_dev_lib\SDL2-2.0.4i686\lib/libSDL2main.a(SDL_windows_main.o): In function `main_utf8':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/main/windows/SDL_windows_main.c:126: undefined reference to `SDL_main'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/main/windows/SDL_windows_main.c:126: undefined reference to `SDL_main'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/main/windows/SDL_windows_main.c:126: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
Makefile:29: recipe for target 'all' failed
mingw32-make: *** [all] Error 1

Makefile

#OBJS specifies which files to compile as part of the project

OBJS = $(wildcard ./neo/engine/**/*.cpp) 

#CC specifies which compiler we're using
CC = g++

#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -IC:\\mingw_dev_lib\\SDL2-2.0.4i686\\include

#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -LC:\mingw_dev_lib\\SDL2-2.0.4i686\lib

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS = -w -Wl,-subsystem,windows

SDL_LIBS = sdl-config --libs

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32  -lSDL2main -lSDL2 -lopengl32 -lglu32

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = pixelHell

#This is the target that compiles our executable
all : $(OBJS)
    $(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)


clean:
    rm -f $(OBJ_NAME).exe

main.cpp

#include "gamesys/EngineCore.hpp"

extern "C" //<---- Thought that would help...
int main(int argc, char *argv[])
{
    phEngineCore* engine = new phEngineCore();
    engine->setWindowTitle("Pixel Hell Engine 0.0.1");

    /* Initalize the game engine. */
    if(!engine->initalize())
        return 1;

    /* Run framework. */
    engine->run();

    /* Clean up memory when finished! */
    engine->clearAll();

    delete engine;

    return 0;
}

Top of EngineCore.hpp

#ifndef ENGINE_CORE_HPP
#define ENGINE_CORE_HPP

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <stdio.h>
#include <string>
#include <vector>

#undef main //....
ajm113
  • 936
  • 1
  • 8
  • 18

2 Answers2

9

As from the documentation:

Make sure that you are declaring main() as:

#include "SDL.h"

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

Your main.cpp file does not look right indeed.
See the above mentioned link for further details.

Community
  • 1
  • 1
skypjack
  • 49,335
  • 19
  • 95
  • 187
3

Ah need to change line 3 on may Makefile to:

OBJS = $(wildcard ./neo/engine/*.cpp)  $(wildcard ./neo/engine/**/*.cpp) 

I would assumed the /**/*.cpp would have fixed that...

ajm113
  • 936
  • 1
  • 8
  • 18
  • 1
    Man I love spending 1-2 hours troubling shooting an issue that was simply a config mess up in my Makefile. – ajm113 Mar 07 '16 at 00:57