The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
dependency on mingw32 lib on MinGW w64 build environment
qzole


Joined: 02 Jan 2016
Posts: 7
Hi!

I started using SDL2 for a new project. Wanted to use a c++ wrapper, so started using SDL2pp, but got into a bit of complication with building on Windows with MinGW.
After some discussion, we somewhat solved the problem, but a curious question remained open.

Why is the -lmingw32 flag needed for compilation of projects on windows with MinGW?
Without it, there is an undefined reference for WinMain, even if -lSDL2main is used.
The most bizarre thing for me is that the undefined reference originates from libmingw32.a!

Code:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status


Here is the complete discussion on SDL2pp's github page: https://github.com/libSDL2pp/libSDL2pp/issues/66

Thanks for anyone who can help!
Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
bug in SDL2pp, not SDL2. And libmingw32 is the C runtime, mingw links that by default, so their build system is seriously messed up.
Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
or perhaps SDL2main is linked BEFORE default libs somehow?
qzole


Joined: 02 Jan 2016
Posts: 7
Sorry, I think I wasn't clear, it's independent of SDL2pp.

Given a simple SDL2 program:
Code:
#include <iostream>
#include <SDL.h>

int main(int, char*[]) {
   if (SDL_Init(SDL_INIT_VIDEO) == 0) {
      std::cout << "Initialized SDL2" << std::endl;
      SDL_Quit();
      return 0;
   } else {
      std::cout << "Couldn't initialize SDL2: " << SDL_GetError() << std::endl;
      return 1;
   }
}


I can compile this with a CMake, using pkg-config to find the SDL2 dependency. The compilation output with make VERBOSE=1:
Code:
$ make VERBOSE=1
/C/msys64/mingw64/bin/cmake.exe -H/E/workspace/hello_sdl2 -B/E/workspace/hello_sdl2/build --check-build-system CMakeFiles/Makefile.cmake 0
/C/msys64/mingw64/bin/cmake.exe -E cmake_progress_start /E/workspace/hello_sdl2/build/CMakeFiles /E/workspace/hello_sdl2/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/e/workspace/hello_sdl2/build'
make -f CMakeFiles/HelloSDL2.dir/build.make CMakeFiles/HelloSDL2.dir/depend
make[2]: Entering directory '/e/workspace/hello_sdl2/build'
/C/msys64/mingw64/bin/cmake.exe -E cmake_depends "MSYS Makefiles" /E/workspace/hello_sdl2 /E/workspace/hello_sdl2 /E/workspace/hello_sdl2/build /E/workspace/hello_sdl2/build /E/workspace/hello_sdl2/build/CMakeFiles/HelloSDL2.dir/DependInfo.cmake --color=
Dependee "E:/workspace/hello_sdl2/main.cpp" is newer than depends file "E:/workspace/hello_sdl2/build/CMakeFiles/HelloSDL2.dir/depend.internal".
Clearing dependencies in "E:/workspace/hello_sdl2/build/CMakeFiles/HelloSDL2.dir/depend.make".
Scanning dependencies of target HelloSDL2
make[2]: Leaving directory '/e/workspace/hello_sdl2/build'
make -f CMakeFiles/HelloSDL2.dir/build.make CMakeFiles/HelloSDL2.dir/build
make[2]: Entering directory '/e/workspace/hello_sdl2/build'
[ 50%] Building CXX object CMakeFiles/HelloSDL2.dir/main.cpp.obj
/C/msys64/mingw64/bin/g++.exe    -I/C/msys64/mingw64/include/SDL2  -Wall -Wextra -pedantic -std=c++14   -o CMakeFiles/HelloSDL2.dir/main.cpp.obj -c /E/workspace/hello_sdl2/main.cpp
[100%] Linking CXX executable HelloSDL2.exe
/C/msys64/mingw64/bin/cmake.exe -E remove -f CMakeFiles/HelloSDL2.dir/objects.a
/C/msys64/mingw64/bin/ar.exe cr CMakeFiles/HelloSDL2.dir/objects.a "CMakeFiles/HelloSDL2.dir/main.cpp.obj"
/C/msys64/mingw64/bin/g++.exe   -Wall -Wextra -pedantic -std=c++14   -Wl,--whole-archive CMakeFiles/HelloSDL2.dir/objects.a -Wl,--no-whole-archive  -o HelloSDL2.exe -Wl,--major-image-version,0,--minor-image-version,0 -lmingw32 -lSDL2main -lSDL2 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
make[2]: Leaving directory '/e/workspace/hello_sdl2/build'
[100%] Built target HelloSDL2
make[1]: Leaving directory '/e/workspace/hello_sdl2/build'
/C/msys64/mingw64/bin/cmake.exe -E cmake_progress_start /E/workspace/hello_sdl2/build/CMakeFiles 0


My question is why is -lmingw32 among the linker flags, why is it needed? Shouldn't it be linked by default?
Trying to minimize the compilation, this is what works:
Code:
 g++ -I/C/msys64/mingw64/include/SDL2 ../main.cpp -lmingw32 -lSDL2main -lSDL2


But if I leave out -lmingw32, then the undefined error comes even though SDL2main is linked:
Code:
$ g++ -I/C/msys64/mingw64/include/SDL2 ../main.cpp -lSDL2main -lSDL2
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status


So any ideas? Or should I search around MinGW?
qzole


Joined: 02 Jan 2016
Posts: 7
One important information that I forgot to mention.
I'm using SDL version 2.0.3
dependency on mingw32 lib on MinGW w64 build environment
Dmitry Marakasov
Guest

* Nathaniel J Fries wrote:

Quote:
bug in SDL2pp, not SDL2. And libmingw32 is the C runtime, mingw
links that by default, so their build system is seriously messed
up.

There's not SDL2pp problem. For instance, FindSDL (for SDL 1.2)
module bundled with CMake does add -lmingw32 explicitly. Also,
sdl-config also lists this library for some reason. I'd really like
to know why it's needed.

--
Dmitry Marakasov . 55B5 0596 FF1E 8D84 5F56 9510 D35A 80DD F9D2 F77D
..: jabber: http://amdmi3.ru
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org