2

I'm trying to compile and run the following code:

#include <iostream>
#include <SDL.h>
#include <SDL_net.h>
#include <cstring>


using namespace std;

int main(int argc, char **argv)
{
    printf("result of SDL_Init is: %i\n",SDL_Init(SDL_INIT_EVERYTHING));
    printf("result of SDLNet_Init is: %i\n",SDLNet_Init());
}

The code compiles fine, but when I run it I get an error:

The application was unable to start correctly (0xc000007b). Click OK to close the application

I've successfully compiled and run SDL code before, but this is my first time trying out SDL_Net.

I'm using Code::Blocks on Windows with MinGW GCC compiler, I'm using the x86_64-w64-mingw32 libraries for SDL, and these are my linker settings (some of which are not immediately necessary, I know):

-lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lpthread -lSDL2_net

Does anyone know what's going on here?

//ETA:

How can I resolve this issue without switching all my stuff over to 32-bit?

Thanks,

boxcartenant
  • 271
  • 2
  • 14
  • 5
    Possible duplicate of [Debugging load time error in C++ SDL2 program compiled with VS2015 on Win10](https://stackoverflow.com/questions/37872877/debugging-load-time-error-in-c-sdl2-program-compiled-with-vs2015-on-win10) and https://stackoverflow.com/questions/49559675/the-application-was-unable-to-start-correctly-0xc000007b-error-when-working-wi – 273K May 18 '18 at 18:12
  • Thanks for the links. The solutions in those threads involved changing the application to 32 bit. What if I want a 64 bit application? – boxcartenant May 18 '18 at 18:22
  • OK, so I just tried switching my compiler over to MinGW-W64, and setting that as the project compiler, but there's no change in the results of compilation. – boxcartenant May 18 '18 at 19:10
  • Last note on this, I guess, but I notice that my Q has been marked as a duplicate. That's my fault for asking the vague question "what's going on here?" which is indeed answered elsewhere. I was hoping to receive an answer to, "How do I fix this?". – boxcartenant May 21 '18 at 22:13
  • @S.M. The question is clarified in the //ETA section. I do not want to switch my application to 32 bit, as they did the answer on the other post. Also, I'm compiling in Code::Blocks, and the other answers have to do with VS. It's not a duplicate, because those answers do not apply to my question. I don't know how to unmark this as duplicate, but please do so, or explain better how mine is actually a duplicate. – boxcartenant Aug 17 '18 at 18:36

1 Answers1

2

I solved the issue by the following:

  1. Installed mingw-w64 instead of mingw32 and set that as my compiler. I had to change most of the toolchain executables so that they pointed to files with "x86_64-w64-mingw32-" in the name (for example, "gcc.exe" was changed to "x86_64-w64-mingw32-gcc.exe"). the Resource Compiler and Make program names were unchanged.

  2. Using the binaries in the x86_x64-mingw32 folder of the SDL_Net development library did not work. I had to replace them with the SDL2_net-2.0.1-win32-x64 runtime binaries that you can download directly on the SDL_Net homepage.

//Note: Maybe the "i686-w64-mingw32" development library binaries would have worked... I didn't test it. I always get confused when "64" is prefaced by "86" and "686", and in the case of these dev libraries, the other option is "x86_64-w64-mingw32".... so which one is really x64, and which one is x32? Not sure.

  1. After I made that change, I stopped getting the titular error in my original post. Instead, now when I try to run the compiled program, I would get errors about missing DLLs. Further, these errors only happened when I ran the program via the executable in "debug"; the errors didn't happen when I ran the program by pressing the "play" button in Code::Blocks. After some hunting, I solved this problem by changing my linker options to the following:

-lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_net -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic

//Note: I don't actually fully understand what "-Wl,-Bstatic" and "-Wl,-Bdynamic" do. I wish someone would explain that to me.

I hope this helps somebody!

boxcartenant
  • 271
  • 2
  • 14
  • Huh. `x86_64-w64-mingw32` libraries are compiled for 64 bits, how did they even work for you before if you had mingw32. *"so which one is really x64, and which one is x32"* `x86_64` is x64, `i686` is x32. – HolyBlackCat May 18 '18 at 22:51
  • Good observation. Yeah, I'm really not sure, lol. – boxcartenant May 21 '18 at 22:10
  • 1
    "-Wl,-Bstatic" and "-Wl,-Bdynamic" are toggle flags sent to the linker. Since your build command is doing both compiling and linking, the "-Wl," is prefixed to indicate that this is a flag intended just for the linker. -Bstatic means that all libraries specified after (using -l) are to be linked statically. -Bdynamic (or the equivalent -Bshared) means that all libraries after will be linked dynamically (.dll or .so needed). – Jonny D May 10 '22 at 15:11
  • @JonnyD Wow this is really good information! Thanks! Is there a technical resource out there which exhaustively lists and describes linker syntax and commands such as these? Where can I go to learn this? – boxcartenant Jun 10 '22 at 15:04
  • @boxcartenant Sometimes I use the manpage for things like this on Linux. Just fighting through compilation/Makefile issues over the years eventually gets you there, FWIW. Some IDEs let you browse the compiler flags they support and see the command-line. I don't have a go-to reference, but something like this might help: https://caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html – Jonny D Jun 17 '22 at 14:29