2016-03-06 38 views
1

我試圖在我的Windows 10 64位機器上編譯我的SDL項目。不幸的是,在編譯時使用mingw32-make。我的編譯在鏈接拋出舊的「未定義的引用到SDL_main」之前就失敗了。我確定我使用了mingw開發庫「i686-w64-mingw32」。 (我也切換到「x86_64-w64-mingw32」,但結果相同。)和#undef main包含SDL後。Mingw32中對'SDL_main'的未定義引用

這裏是運行的mingw32-化妝後的控制檯輸出...

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; 
} 

頂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 //.... 

回答

1

啊需要更改線路3可生成文件:

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

我會假設/**/*.cpp將有固定的...

+0

我喜歡花費1-2個小時的時間來煩惱拍攝一個簡單的問題,這個問題在我的Makefile中很糟糕。 – ajm113

2

由於從documentation

確保您聲明的main()爲:

#include "SDL.h" 

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

main.cpp文件看起來不正確的確實。
有關更多詳細信息,請參閱上述鏈接。

+0

是的,試過了。即使我沒有在我的主要SDL電話。 – ajm113

+0

甚至在int main之前嘗試過#undef main只是爲了確定。 – ajm113

+0

這個答案解決了它,儘管我沒有真正看到我的應用程序中需要任何參數,謝謝。 +1 –