New Learner: SDL test project won't run

I am new to programming and finishing up a beginner C++ course. The final project is to build an SDL project. I got the project to build, finally, but it doesn’t run. Nothing happens when I try to run it.
I’m using Eclipse 2020-06 and MinGW-w64 on a 64 bit Windows 10 machine.

  1. #include

  2. #define SDL_MAIN_HANDLED

  3. #include <SDL.h>

  4. using namespace std;

  5. int main(int, char**) {

  6.              if (SDL_Init (SDL_INIT_VIDEO) < 0) {
  7.                        cout << "SDL_Init failed." << endl;
  8.                        return 1;
  9.            }
  10.          cout << "SDL_Init succeeded." << endl;
  11.          SDL_GetError ();
  12.          SDL_Quit ();
  13.          return 0;
  14. }

Thank you for your help.

HEllo and welcome to SDL discourse.

First, it should be very good if you walk though the discourse tutorial and learn how to paste a source code. (spoiler, just select the code and press the icon </>).

I strongly recommend you to follow up lazyfoo’s SDL tutorial .

There are many issues in your code, for example. SDL_GetError(); should be called to get a error. That is, it should be called inside your if statement to check for errors. (I don’t even know how it behaves if there is no error.

This code must do something at least print SDL_Init failed or SDL_Init succeeded.

IMHO, your int main() call should contain variable names as

int main(int argc, char *argv[])

Also calls to using namespace std; are not recommended.

Last but not least, which SDL version are you using? SDL2? Never heard of SDL_MAIN_HANDLED macro. Anyhow, I was able to compile a look a like code here and run it successfully.

#include <SDL2/SDL.h>

int main(int argc, char *argv[]) {
    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
        SDL_Log("Failure %s\n", SDL_GetError());
        return 1;
    return 0;

And the output is:

$ ./a.out
INFO: Success

$ unset DISPLAY
$ ./a.out
INFO: Failure No available video device

Thank you for your feedback. I will read through the discourse tutorial more thoroughly and also read through lazyfoo’s tutorial. I did look at it, but just briefly.

I thought that I had mentioned that I was following a tutorial - a Beginner’s Course on Except for the GetError, all the other code that I used came from the course. The instructor uses a Mac, so those of us on Windows have to figure out the differences for ourselves. I got the idea of using GetError from a post I read somewhere (thank you for instructing me on it’s PROPER use.) Same thing with the variable names in int main (). The instructor didn’t put anything there. I was just trying different things to see if I could get the code to run.
Yes, I am using SDL2 and the macro was suggested by the teacher assistant who answers our questions about the tutorials. I haven’t told him yet that the code isn’t working. I wanted to try researching and getting it working on my own first. But there is no error message to research, nothing at all happens.
Again, thank you.

Seems this is not a very good course. Follow along lazyfoo’s and you will be happy.

:pensive: Noted. Two questions please:

Where do I find the discourse tutorials? I found the Guidelines, but I don’t see tutorials.

When someone refers to “the same directory as your executable”, are they talking about the folder with the workspace_cpp or the actual project folder (src folder) or someplace else entirely?

15 mins after I posted this, I found a YouTube tutorial that helped me answer the above question. It turns out that I had forgotten to copy the SDL2.dll file to the MinGW64 bin folder. The project runs now and I don’t need the #define macro!!

Thank you once again for your time and feedback.

I am back again because the project compiled and ran, but only one time. When I tried running it again, this error came up: C:/MinGW64/bin/…/lib/gcc/x86_64-w64-mingw32/8.1.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: cannot open output file SDL_Test.exe: Invalid argument. :scream_cat:

Same place as executable is where your final .exe will be. the final destination depends on how you build your project (sorry I know nothing about building in windows).

The discourse tutorial shows up when you create an account in this site, it guides you through the features of this forum, how to post, how to quote, how to reply. and things like that.

About your ld.exe error, sorry, I can’t help you with building SDL code in windows.

As a guideline, open a new topic for each issue you have, will get the proper attention, I could help you with your code but I can’t help you with build on windows, creating a new topic would help.

Anyway I would like to advice you, follow up LazyFoo’s tutorial, he has a guide in how to build SDL binaries on windows. (