-2

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 
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • To use [`UuidCreate`](https://learn.microsoft.com/en-us/windows/win32/api/rpcdce/nf-rpcdce-uuidcreate#requirements) you also need to link with `Rpcrt4.lib` or the MinGW equivalent. – Richard Critten Mar 11 '23 at 01:13
  • [`SetupDiGetClassDevsA`](https://learn.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetclassdevsexa#requirements) needs `Setupapi.lib` – Richard Critten Mar 11 '23 at 01:16
  • Hi Richard, why is it working without -static and stop working with adding -static? (there are many errors, not only UuidCreat) Also project is working brilliantly when I compile on mac using g++-12 – k-irsan_kaifatmailru Mar 11 '23 at 01:16
  • @k-irsan_kaifatmailru dynamic libraries have depenency information built into them so linker can recursively pull dependencies; static libraries don't. `-static` means "build static executable", without *any* use of dynamic libraries. If you only want gcc's specific libs, you could use `-static-libgcc -static-libstdc++` instead. – keltar Mar 11 '23 at 04:50
  • @keltar If you check my make you can see that -static-libstdc++ is already added and this does not help - program still asking for libstdc++-6.dll (if I compile without -static). How to fix my specific problem? – k-irsan_kaifatmailru Mar 11 '23 at 13:45
  • @k-irsan_kaifatmailru use `g++` instead of `gcc` as linking command, and don't pass `-lstdc++` argument. It can be done with gcc and `-lstdc++` but it will require some trickery with linker options (e.g. `-Wl,-Bstatic` option in the right place). – keltar Mar 11 '23 at 14:35
  • @keltar Thank you, it looks like we are on the right track, I followed your advice, and now .exe stop asking for libstdc++-6.dll, but now it is asking for libwinpthread-1.dll (but it seems it is the last step to fix to become playable) I have downloaded libwinpthread-1.dll but that did not help - another error appeared. – k-irsan_kaifatmailru Mar 11 '23 at 14:52
  • @keltar I solved it, adding "-Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive" helped, now my .exe is working on windows. I can add working makefile if needed. – k-irsan_kaifatmailru Mar 11 '23 at 17:36

1 Answers1

0

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)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '23 at 19:56