0

I'm trying to run a simple code that includes the "SDL.h" file, just to verify it works properly and it gives me no errors, but it's not like that. I'm using VS Code and MinGW compiler. Here is the code:

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

int main (int arcg, char* arcv[]) {
    std::cout << "it works";
    return 0;
}

I've installed all the SDL2 stuff and I put all the headers i need in a "sdl" folder, and this is my makefile:

all:
g++ -Iinclude -Iinclude/sdl -Iinclude/headers -Llib -o Main src/*.cpp -lmingw32 -lSDL2main -lSDL2 -lSDL2_image

The inconsistency is that while I'm writing the code the compiler doesn't give me errors (in other similar situations it does (the red underline), for example if I try to include a .h that doesn't exist) and when i digit "#include" it shows me all the files that i can include from the "sdl" folder, but when i run the program the error occurs:

main.cpp:2:10: fatal error: SDL.h: No such file or directory
2 | #include <SDL.h>
  |          ^~~~~~~

Someone help me

This is the directory structure pointed by tree:

.
├── Main.exe
├── MakeFile
├── SDL2.dll
├── SDL2_image.dll
├── include
│   ├── headers
│   └── sdl
│       ├── SDL.exe
│       ├── SDL.h
│       ├── SDL_assert.h
│       ├── SDL_atomic.h
│       ├── SDL_audio.h
│       ├── SDL_bits.h
│       ├── SDL_blendmode.h
│       ├── SDL_clipboard.h
│       ├── SDL_config.h
│       ├── SDL_cpuinfo.h
│       ├── SDL_egl.h
│       ├── SDL_endian.h
│       ├── SDL_error.h
│       ├── SDL_events.h
│       ├── SDL_filesystem.h
│       ├── SDL_gamecontroller.h
│       ├── SDL_gesture.h
│       ├── SDL_haptic.h
│       ├── SDL_hidapi.h
│       ├── SDL_hints.h
│       ├── SDL_image.h
│       ├── SDL_joystick.h
│       ├── SDL_keyboard.h
│       ├── SDL_keycode.h
│       ├── SDL_loadso.h
│       ├── SDL_locale.h
│       ├── SDL_log.h
│       ├── SDL_main.h
│       ├── SDL_messagebox.h
│       ├── SDL_metal.h
│       ├── SDL_misc.h
│       ├── SDL_mouse.h
│       ├── SDL_mutex.h
│       ├── SDL_name.h
│       ├── SDL_opengl.h
│       ├── SDL_opengl_glext.h
│       ├── SDL_opengles.h
│       ├── SDL_opengles2.h
│       ├── SDL_opengles2_gl2.h
│       ├── SDL_opengles2_gl2ext.h
│       ├── SDL_opengles2_gl2platform.h
│       ├── SDL_opengles2_khrplatform.h
│       ├── SDL_pixels.h
│       ├── SDL_platform.h
│       ├── SDL_power.h
│       ├── SDL_quit.h
│       ├── SDL_rect.h
│       ├── SDL_render.h
│       ├── SDL_revision.h
│       ├── SDL_rwops.h
│       ├── SDL_scancode.h
│       ├── SDL_sensor.h
│       ├── SDL_shape.h
│       ├── SDL_stdinc.h
│       ├── SDL_surface.h
│       ├── SDL_system.h
│       ├── SDL_syswm.h
│       ├── SDL_test.h
│       ├── SDL_test_assert.h
│       ├── SDL_test_common.h
│       ├── SDL_test_compare.h
│       ├── SDL_test_crc32.h
│       ├── SDL_test_font.h
│       ├── SDL_test_fuzzer.h
│       ├── SDL_test_harness.h
│       ├── SDL_test_images.h
│       ├── SDL_test_log.h
│       ├── SDL_test_md5.h
│       ├── SDL_test_memory.h
│       ├── SDL_test_random.h
│       ├── SDL_thread.h
│       ├── SDL_timer.h
│       ├── SDL_touch.h
│       ├── SDL_types.h
│       ├── SDL_version.h
│       ├── SDL_video.h
│       ├── SDL_vulkan.h
│       ├── begin_code.h
│       └── close_code.h
├── lib
│   ├── cmake
│   │   └── SDL2
│   │       ├── sdl2-config-version.cmake
│   │       └── sdl2-config.cmake
│   ├── libSDL2.a
│   ├── libSDL2.dll.a
│   ├── libSDL2.la
│   ├── libSDL2_image.a
│   ├── libSDL2_image.dll.a
│   ├── libSDL2_image.la
│   ├── libSDL2_test.a
│   ├── libSDL2_test.la
│   ├── libSDL2main.a
│   ├── libSDL2main.la
│   └── pkgconfig
│       ├── SDL2_image.pc
│       └── sdl2.pc
├── libjpeg-9.dll
├── libpng16-16.dll
├── libtiff-5.dll
├── libwebp-7.dll
├── src
│   └── main.cpp
└── zlib1.dll

8 directories, 103 files

And this is the new makefile:

all:
    g++ -Iinclude -Iinclude/sdl -Iinclude/headers -Llib -o Main src/*.cpp -lmingw32 -lSDL2main -lSDL2 -lSDL2_image 
    tree .
wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 1
    The compiler command looks ok. Can you confirm that you have `SDL.h` in one of the directories passed to `-I`? Not in subdirectories. – HolyBlackCat Mar 13 '22 at 14:33
  • Since `SDL.h` is shipped in a directory called `SDL2`, I'd expect one of the paths passed to `-I` to end with `SDL2`. – HolyBlackCat Mar 13 '22 at 14:49
  • thank you for the asnwer, I have SDL.h in the subdirectory "sdl" (which is in the directory "include") but i passed include/sdl – SirrrRoland Mar 13 '22 at 15:04
  • It should work then. You're not telling us something. – HolyBlackCat Mar 13 '22 at 15:05
  • I really don't know – SirrrRoland Mar 13 '22 at 15:14
  • It may be a lot simpler for you if you switch to use msys2 and have it install SDL2 properly using its package manager. Then you don't need to set the path since it will be in the system paths that your compiler already searches. https://www.msys2.org/ – drescherjm Mar 13 '22 at 15:28
  • @drescherjm thank you, i will try. But I have to understand why it does so. – SirrrRoland Mar 13 '22 at 15:43
  • It could help us if you show the lines above: `main.cpp:2:10: fatal error: SDL.h: No such file or directory` so we can see the exact build command used. – drescherjm Mar 13 '22 at 15:46
  • ***the compiler doesn't give me errors (in other similar situations it does (the red underline)*** Red underlines are independent from your build task. Intellisense and building are handled and configured separately. – drescherjm Mar 13 '22 at 15:47
  • Can you show the contents of `include/sdl` to us? – HolyBlackCat Mar 13 '22 at 15:52
  • @drescherjm these are the lines above: [Running] cd "c:\Users\giordano\Desktop\project\src\" && g++ main.cpp -o main && "c:\Users\giordano\Desktop\project\src\"main – SirrrRoland Mar 13 '22 at 16:00
  • @HolyBlackCat i don't know how to show you the contents – SirrrRoland Mar 13 '22 at 16:10
  • Figure something out. You could at least list the filenames inside. – HolyBlackCat Mar 13 '22 at 16:11
  • @HolyBlackCat begin_code close_code SDL SDL_assert SDL_atomic SDL_audio SDL_bits SDL_blendmode SDL_clipboard SDL_config SDL_cpuinfo SDL_egl SDL_endian SDL_error SDL_events SDL_filesystem SDL_gamecontroller SDL_gesture SDL_haptic SDL_hidapi SDL_hints SDL_image SDL_joystick SDL_keyboard SDL_keycode SDL_loadso SDL_locale SDL_log SDL_main SDL_messagebox SDL_metal SDL_misc SDL_mouse SDL_mutex SDL_name SDL_opengl SDL_opengl_glext SDL_opengles SDL_opengles2 SDL_opengles2_gl2 SDL_opengles2_gl2ext SDL_opengles2_gl2platform SDL_opengles2_khrplatform – SirrrRoland Mar 13 '22 at 16:26
  • SDL_pixels SDL_platform SDL_power SDL_quit SDL_rect SDL_render SDL_revision SDL_rwops SDL_scancode SDL_sensor SDL_shape SDL_stdinc SDL_surface SDL_system SDL_syswm SDL_test SDL_test_assert SDL_test_common SDL_test_compare SDL_test_crc32 SDL_test_font SDL_test_fuzzer SDL_test_harness SDL_test_images SDL_test_log SDL_test_md5 SDL_test_memory SDL_test_random SDL_thread SDL_timer SDL_touch SDL_types SDL_version SDL_video SDL_vulkan – SirrrRoland Mar 13 '22 at 16:27
  • Hmm, this looks ok. You're making some simple mistake - either this is not the actual compiler command you're using, or you're running it from a wrong directory, or something else. Here's what I suggest: [install MSYS2](https://stackoverflow.com/q/30069830/2752075), from it install the compiler, `make` and `tree`, add `tree .` next to the compiler command to print the directory contents, and run the makefile again (from MSYS2 shell this time). Then show what `tree` prints. – HolyBlackCat Mar 13 '22 at 16:29
  • @HolyBlackCat okay thank you, I will try tomorrow – SirrrRoland Mar 13 '22 at 16:46
  • When you try tomorrow my advice is to put the additional information in your question. It's difficult to read it in the comments. – drescherjm Mar 13 '22 at 22:25
  • @HolyBlackCat I'm sorry I'm late, i've installed MSYS2, the compiler, make and tree, but I don't know how to do the rest (adding tree . next to the compiler command and running the makefile), how can I do it? – SirrrRoland Mar 18 '22 at 14:40
  • Below the `g++ ...` line in the makefile, add `tree .`. Note that those two lines have to have a single 'tab' at the left. Then run the makefile as usual and show the directory structure printed by `tree`. – HolyBlackCat Mar 18 '22 at 17:11
  • @HolyBlackCat okay I did it editing the question above – SirrrRoland Mar 18 '22 at 20:43
  • There's something you're not telling us, it should work. Can you show the new makefile? How exactly do you run it? Was the `tree` output produced by running the makefile with the `tree .` command in it, or by you manually calling `tree`? – HolyBlackCat Mar 18 '22 at 20:46
  • @HolyBlackCat Added above – SirrrRoland Mar 18 '22 at 22:36
  • The `cd "...\src" && g++ main.cpp -o main` suggests it's not the `make all` command you've shown actually being run. – aschepler Aug 06 '22 at 13:10

0 Answers0