c++makefilecross-platformsdlmingw32

Cannot -static compile project for windows using mingw32 (x86_64-w64-mingw32-gcc) on mac (libstdc++-6.dll )


I have a C++ game using SDL2 and a lot of C++-12 libraries on macOS Big Sur. I can compile it with x86_64-w64-mingw32-gcc, but then .exe file on windows asking for libstdc++-6.dll (also for SDL dlls, but I can download them), downloading libstdc++-6.dll not helping because it gives more new errors if run .exe.

I made my research and the suggestion was to add -static compilation option, however that lead for another error preventing compilation: "undefined reference to"

Please see my Makefile:

#OBJS specifies which files to compile as part of the project
OBJS = strategy.cpp

#CC specifies which compiler we're using x86_64-w64-mingw32-gcc
CC = x86_64-w64-mingw32-gcc

#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -I /Users/jirnyak/Mirror/DND/mingw_dev_lib/include/SDL2

#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -L /Users/jirnyak/Mirror/DND/mingw_dev_lib/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

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_image -lstdc++ 

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = dukalop_win

#This is the target that compiles our executable
all : $(OBJS)
    $(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) `/Users/jirnyak/Mirror/DND/mingw_dev_lib/bin/sdl2-config --cflags` `/Users/jirnyak/Mirror/DND/mingw_dev_lib/bin/sdl2-config --libs` -static-libstdc++ -static -o $(OBJ_NAME) 

Tried:

  1. Download libstdc++-6.dll -> Fail to run .exe with more errors

  2. Add -static compilation -> Fail of compilation with many "undefined reference to" errors (for example:

    ld: /Users/jirnyak/Mirror/DND/mingw_dev_lib/lib/libSDL2.a(SDL_hidapi.o): in function `PLATFORM_hid_enumerate':
    /Users/valve/release/SDL2/SDL2-2.26.4-source/foo-x64/../src/hidapi/windows/hid.c:387: undefined reference to `__imp_SetupDiGetClassDevsA'
    ...
    **many similar errors**
    ...
    /opt/local/lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld: /Users/jirnyak/Mirror/DND/mingw_dev_lib/lib/libSDL2_ttf.a(libSDL2_ttf_la-hb-uniscribe.o): in function `_hb_generate_unique_face_name':
    /Users/valve/release/SDL_ttf/SDL2_ttf-2.20.2-source/foo-x64/../external/harfbuzz/src/hb-uniscribe.cc:306: undefined reference to `__imp_UuidCreate'
    collect2: error: ld returned 1 exit status
    make: *** [all] Error 1
    

I expect someone to suggest me some ideas how to link everything correctly or solve that libstdc++-6.dll error.

It is always working on Mac when I am compiling using g++-12 (I have cross platform problem to make .exe for Windows):

g++-12 strategy.cpp -o strategy -I /Library/Frameworks/SDL2.framework/Headers -F /Library/Frameworks -framework SDL2 -I /Library/Frameworks/SDL2_image.framework/Headers -F /Library/Frameworks -framework SDL2_image -I /Library/Frameworks/SDL2_ttf.framework/Headers -F /Library/Frameworks -framework SDL2_ttf -fopenmp -std=c++11 

Solution

  • With help from @keltar, I have build my program, working make file:

    #OBJS specifies which files to compile as part of the project
    OBJS = strategy.cpp
    
    #CC specifies which compiler we're using
    CC = x86_64-w64-mingw32-g++
    
    #INCLUDE_PATHS specifies the additional include paths we'll need
    INCLUDE_PATHS = -I /Users/jirnyak/Mirror/DND/mingw_dev_lib/include/SDL2
    
    #LIBRARY_PATHS specifies the additional library paths we'll need
    LIBRARY_PATHS = -L /Users/jirnyak/Mirror/DND/mingw_dev_lib/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
    
    #LINKER_FLAGS specifies the libraries we're linking against 
    LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_image -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive 
    
    #OBJ_NAME specifies the name of our exectuable
    OBJ_NAME = dukalop_win
    
    #This is the target that compiles our executable
    all : $(OBJS)
        $(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)