0

I am developing an application (computer game) that will run on both Linux and Windows. My development is performed in Linux and for the Windows version I cross-compile in my Linux env using i686-w64-mingw32-g++. I would like the program to dump out the stack trace when it encounters a segmentation fault, I know how to do this for the Linux version of the program using backtrace but I am really struggling with how to do this on Windows.

I have looked at some examples suggestions here and here, but they seem to assume you are compiling your application code from a Windows platform, as they link dbghelp.dll when compiling (I assume you can't link dll files on Linux? Is there a mingw version of this dll?). I also tried using boost::stacktrace but that just prints gibberish on crashing in Windows.

Or am I going about this the wrong way completely? Basically I want to be able to dump a stack trace upon an error in the application (such as a segmentation fault) for the Windows version of the application but I am developing the application in a Linux environment.

lancen
  • 9
  • 2
  • Of course you can link to Windows DLLs when cross-compiling from a non-Windows system. What your app links to has nothing to do with the environment it is built in. Your Windows app has to link to Windows system DLLs anyway (like kernel32.dll, and commonly advapi32.dll, user32.dll, etc) just to run on Windows at all. – Remy Lebeau Apr 26 '23 at 20:54
  • https://learn.microsoft.com/en-us/windows/win32/api/minidumpapiset/nf-minidumpapiset-minidumpwritedump – Paul Sanders Apr 26 '23 at 21:38
  • Just to mention that since C++23 stack trace has been standardized as [`std::basic_stacktrace`](https://en.cppreference.com/w/cpp/utility/basic_stacktrace) – Karen Baghdasaryan Apr 26 '23 at 22:37
  • `boost::stacktrace` shouldn't crash, maybe you could post a simple reproducer? – ssbssa Apr 27 '23 at 10:49
  • As for a mingw version of `dbghelp.dll`, you can try [`mgwhelp.dll`](https://github.com/jrfonseca/drmingw#mgwhelp). – ssbssa Apr 27 '23 at 11:00
  • @RemyLebeau, I am not familiar with Windows development, for example, when I tried following the instructions [here](https://stackoverflow.com/questions/5693192/win32-backtrace-from-c-code), I get the message: `undefined reference to _imp__SymInitialize@12` and `undefined reference to _imp__SymFromAddr@20` when I compile my code, so I imagine I need to somehow link the library (dbghelper.dll) in my compile command - how do I do this with a dll from a linux dev env? – lancen Apr 27 '23 at 14:57
  • @lancen You need to add `Dbghelp.lib` to your project, or at least tell your linker to link to it in your build script. That file has the symbols the linker is looking for to make the final exe load the dll functions when it is run on a Windows system. – Remy Lebeau Apr 27 '23 at 20:05
  • @RemyLebeau, that's one of the parts I don't understand, how do I link Dbghelp.dll to my Linux build script? I compile the application from a makefile - it looks something like: `i686-w64-mingw32-g++ -I/usr/local/include -D_REENTRANT -DVERSION= -DREVISION= -DDATA_DIR=\"\" -gdwarf-2 -lefence -std=c++20 -O3 -mwindows -c -o component.cpp` whereby I create the .o files then I link them together via `i686-w64-mingw32-g++ [list of *.o files] -o output.exe -lSDL2_mixer -lSDL2_image -lSDL2_ttf src/ECS/*.cpp -static-libgcc -static-libstdc++ -g` - What is the correct syntax to link windows DLLs? – lancen Apr 29 '23 at 19:48
  • @lancen https://arrayfire.com/blog/cross-compile-to-windows-from-linux/ – Remy Lebeau Apr 29 '23 at 22:30
  • @lancen Just add `-ldbghelp` to the linker line. – ssbssa May 02 '23 at 10:50
  • @ssbssa, this worked for me. But now I am finding that the signal function in C++ doesn't seem to catch segfaults when my application runs in Windows - funny enough it works in Wine but on an actual Win10 desktop it doesn't seem to. – lancen May 11 '23 at 20:13

0 Answers0