Trouble Building on Windows


#1

Can someone help me with setting up build system?
Here is a link to the current status of the project.
https://github.com/ylee95/candycrush/tree/1d4b58bd4244201074d28b42d0f70bca59e3bcdd
and here is my best try for CMakeLists.txt file.

cmake_minimum_required (VERSION 2.8)
project (tamagotchi)

# SDL
add_subdirectory (SDL2-2.0.8 EXCLUDE_FROM_ALL)
target_include_directories (SDL2 PUBLIC $<BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include>)

set (
	SOURCE_FILES
	src/game.cpp
	src/main.cpp
	src/sdlwrapper.cpp
)
add_executable (tamagotchi ${SOURCE_FILES})
target_include_directories (tamagotchi PRIVATE src)
target_link_libraries (tamagotchi SDL2)

The issue is that this project builds fine on linux, but produces linker error on Windows.
I get the following error when I try to build this on Visual Studio 2017

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)	C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CMakeLists.txt	C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\MSVCRTD.lib(exe_winmain.obj)	1

#2

You have to link to SDL2 main.
-lSDL2main

Or you can define SDL_MAIN_HANDLED.
-DSDL_MAIN_HANDLED

Here’s an example makefile:

Summary

INCLUDE_DIR =third-party-mingw\SDL2-2.0.7\i686-w64-mingw32\include\SDL2
CC=mingw32-gcc
CFLAGS=-I$(INCLUDE_DIR)

OBJECT_DIR=obj
SDL_LIB_DIR=third-party-mingw\SDL2-2.0.7\i686-w64-mingw32\lib
SDL_IMAGE_LIB_DIR=third-party-mingw\SDL2_image-2.0.2\i686-w64-mingw32\lib
SDL_MIXER_LIB_DIR=third-party-mingw\SDL2_mixer-2.0.2\i686-w64-mingw32\lib
LIB_DIRS=-L$(SDL_LIB_DIR) -L$(SDL_IMAGE_LIB_DIR) -L$(SDL_MIXER_LIB_DIR)

LIBS=-lm -lSDL2_Image -lSDL2_mixer -lmingw32 -lSDL2 -lSDL2main

_OBJ = aliens.o
OBJ = $(patsubst %,$(OBJECT_DIR)/%,$(_OBJ))

$(OBJECT_DIR)/%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

aliens: $(OBJ)
$(CC) $(CFLAGS) $(OBJECT_DIR)\aliens.o -o aliens.exe $(LIB_DIRS) $(LIBS)

.PHONY: clean

clean:
rm -f $(OBJECT_DIR)/*.o