0

I am new to c++, and I am making my first game using SLD2, with my IDE being VS code

Just to not leave anything out, I will ramble the tale of how this problem came to be:

after setting up everything, I ran the file just to get the error "main.cpp:4:10: fatal error: SDL2/SDL.h: No such file or directory"

So I looked up a tutorial on how to set up SDL2

The tutorial involved setting up a makefile. so I set it up, ran it again, same error. after rewatching the tutorial, i realized I had to run the make command, and to do so I needed a .exe file which I didn't have. so i used the MSYS2 console to install make.exe. it worked, according to the console, but make was nowhere to be found, and neither was "mingw32-make.exe", which is another name for the file.

so I went ahead and installed mingw again, and this time i used the mingw installer instead of msys2, and voila: mingw32-make is now on my computer.

I set the path variable, and went back to my code. ran the make command in the VS code integrated terminal.

then i got the error

"mingw32-make: ***no targets specified and no makefile found"

afterwards I renamed the file to make.

same error, but now it's:

"make: ***no targets specified and no makefile found"

as for my code itself it's just a helloworld script:

#include <iostream>
#include <string>
#include <vector>
#include <SDL2/SDL.h>

int main() {
    std::cout << "Hello World!";
    return 0;
}

could I have done anything wrong?

edit: the makeflie contents are:

all: 
    g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2
thestuff
  • 1
  • 2
  • 4
    *"though I do have a makefile"* In what directory is it, and what are the contents? – HolyBlackCat Feb 24 '23 at 15:02
  • *"SDL2/SDL.h: No such file or directory"* Your problem is that you didn't specify some compiler flag. Reaching for a makefile is a wrong approach, it just makes your life harder (your hammer is broken, and you buy a box to hold more hammers; a makefile is used to run a bunch compilation commands conveniently, but if you don't know what a command should be in the first place, it's useless to you). – HolyBlackCat Feb 24 '23 at 15:05
  • There's a program called `make` that processes so-called makefiles (commonly called `Makefile` or `makefile` without any extension). A makefile contains commands that will invoke the compiler and build the project. So your `make.exe` != makefile. – Friedrich Feb 24 '23 at 15:06
  • About the makefile, it's in C:/Users/[User]/Desktop/myproject, along with my main.cpp file. I've also edited the post to include the contents – thestuff Feb 24 '23 at 15:28
  • 1
    `make` is a separate tool from the compiler. A common mistake is to only install the compiler with msys2. Something along the lines of `pacman -S make` in a msys terminal should take care of the problem. Your life will be a lot easier if you can stay within the msys environment and use it for as much of your tool and library needs as possible. – user4581301 Feb 24 '23 at 15:30
  • 1
    Did you install SDL2 using pacman? If not why didn't you? The installation page is here for mingw x64 SDL2 : [https://packages.msys2.org/package/mingw-w64-x86_64-SDL2?repo=mingw64](https://packages.msys2.org/package/mingw-w64-x86_64-SDL2?repo=mingw64) The install command is on that page `pacman -S mingw-w64-x86_64-SDL2` – drescherjm Feb 24 '23 at 15:31
  • Just to clarify, I know that makefile != make, and I yes I did install make with msys2, and I mentionned that I did nothing – thestuff Feb 24 '23 at 15:32
  • and what is a compiler flag? and how do I install SDL2 using pacman? – thestuff Feb 24 '23 at 15:33
  • 2
    Here's what you should do. 1. Forget about makefiles for now. Uninstall everything: MinGW, SDL2. 2. Install MSYS2 again, as described [here](https://stackoverflow.com/questions/30069830/how-can-i-install-mingw-w64-and-msys2). 3. Use MSYS2 to install both GCC and SDL2: `pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-SDL2`. 4. Run `pkg-config --libs --cflags sdl2`. It will tell you what compiler flags you need to use SDL2. 5. Copy the output, then paste it into the compiler command: `g++ my_code.cpp the_output_you_just_got`. Then it should just work. – HolyBlackCat Feb 24 '23 at 15:36
  • `g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2` doesn't appear to tell the compiler where to find SDL, but that is a future bug. Are you running `make` from the command line or from a tool that may not be using the working directory you expect? – user4581301 Feb 24 '23 at 15:37
  • I'm running it in the VScode integrated terminal – thestuff Feb 24 '23 at 15:38
  • ***I'm running it in the VScode integrated terminal*** You can still do the same build command in VSCode from the instructions from @HolyBlackCat I use pacman inside the mingw64 terminal but after you install the packages in your msys2 then the rest can happen in VSCode. – drescherjm Feb 24 '23 at 15:42
  • 1
    Any success following my suggestion? – HolyBlackCat Feb 24 '23 at 19:21
  • I've ran into another issue. It seems that pkg-config can't even find sdl2.pc ! to (try to) fix that I edited to PKG_CONFIG_PATH variable to be SDL2/lib/pkgconfig with the command `export PKG_CONFIG_PATH= /SDL2/lib`. afterwards, I used the echo function to confirm I didn't do any typos . did nothing. but apparently, using another command , which is `pkg-config --variable pc_path pkg-config`, i found out the first one did nothing at all! and I can't find a way to edit the variable. so I'm stuck at this step. this is too much pain for a coder like me – thestuff Feb 25 '23 at 16:51
  • Plase use `@username` when replying, otherwise we don't get notifications. – HolyBlackCat Feb 26 '23 at 10:41
  • You don't need to edit any environment variables. You're doing something wrong. Note this part in the link I posted: *"If done correctly, the terminal prompt will say UCRT64 in magenta letters, not MSYS."* You either didn't do this, or you installed the wrong SDL2 package (must be `mingw-w64-ucrt-x86_64-SDL2`), or you installed the wrong pkg-config package (must be `mingw-w64-ucrt-x86_64-pkg-config`). – HolyBlackCat Feb 26 '23 at 10:44
  • @HolyBlackCat well, turns out, all this tomfoolery has happened because i installed the wrong library, and used it in the wrong terminal. so thanks – thestuff Feb 26 '23 at 16:33
  • Glad you worked it out. FYI, you can read about different MSYS2 terminal flavors and the respective packages here: https://www.msys2.org/docs/environments/ – HolyBlackCat Feb 26 '23 at 17:15

0 Answers0