0

I am trying to get the header file SDL2/SDL.h to work in C and have the following code:

#include <stdio.h>
#include <SDL2/SDL.h>
#define WIDTH 800
#define HEIGHT 600
#define DELAY 1000

int main (int argc, char **argv)
{
   SDL_Window *window = NULL;

   if (SDL_Init(SDL_INIT_VIDEO) != 0) {
      fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
      return 1;
   }

   window = SDL_CreateWindow("SDL Example", SDL_WINDOWPOS_UNDEFINED, 
            SDL_WINDOWPOS_UNDEFINED, WIDTH, 
            HEIGHT, 0);

   if (window == NULL) {
      fprintf(stderr, "SDL window failed to initialise: %s\n", SDL_GetError());
      return 1;
   }

   SDL_Delay(DELAY);

   SDL_DestroyWindow(window);

   SDL_Quit(); 

   return 0;
}

Note that I have not written this code and I do not understand it 100%. I just wanted something that would work in order to present my question. Anyhow, when I use command prompt to run the program, I enter the following:

gcc -std=c11 main.c -I{Path to SDL2\include} -L{Path to SDL2\lib} -Wall -lmingw32 - lSDL2main -lSDL2 -o main

No errors here, and after I enter:

main.exe

This works just fine, but when I enter the same input as above in Visual Studio Code's terminal there are some problems I wonder about.

Firstly, I need to enter:

.\main.exe

instead of:

main.exe

Why?

Secondly, every time I compile this code I need to enter:

gcc -std=c11 main.c -I{Path to SDL2\include} -L{Path to SDL2\lib} -Wall -lmingw32 - lSDL2main -lSDL2 -o main

(Note that the brackets are not included in the actual code)

However, I am used to only enter:

gcc main.c

followed by:

.\a

But when I enter the former input, I get this error:

C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x15): undefined reference 
to `SDL_Init'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x1e): undefined reference 
to `SDL_GetError'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x78): undefined reference 
to `SDL_CreateWindow'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x86): undefined reference 
to `SDL_GetError'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0xb5): undefined reference 
to `SDL_Delay'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0xc0): undefined reference 
to `SDL_DestroyWindow'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0xc5): undefined reference 
to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o): 
(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

If I had to enter the long input every time I compiled it would be a nightmare! How can avoid this and keep my familiar habit? I use Windows with a 32-bit MinGW compiler on Visual Studio Code (although I have a 64-bit processor).

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    That's what makefiles are for — you simply type `make` and it knows what to build and supplies all the command line options, etc. You have to create the makefile — but only once. – Jonathan Leffler Jun 27 '23 at 22:06
  • Regarding `.\main.exe` vs `main.exe` - that's because you're using a different shell (aka a program that interprets your text commands), probably Powershell vs CMD respectively. Regarding long commands - that's what build systems are for, pick one and use it. The popular ones are Make, CMake, Meson. – HolyBlackCat Jun 27 '23 at 22:08
  • Regarding your compiler: GCC 6 is outdated, the current version is GCC 13. You can get it e.g. from [MSYS2](https://stackoverflow.com/q/30069830/2752075). – HolyBlackCat Jun 27 '23 at 22:09
  • 3
    Do not ask multiple unrelated questions in a single post. Anyone with an answer to only one question will not be able to post a complete answer. – Clifford Jun 27 '23 at 22:24
  • Please delete your [old question](https://stackoverflow.com/questions/76567668/problem-compiling-code-with-sdl2-header-in-c) if this is meant to replace it. Or update your old question and delete this one. – pmacfarlane Jun 27 '23 at 22:33
  • If you use a header file or library you need to tell the compiler where to find them. You can't just say `gcc main.c` if you use extra bits. And Powershell and most Unix shells require a path unless `.` is in the path variable (which is a very bad idea). – Jerry Jeremiah Jun 27 '23 at 22:56

1 Answers1

0

In the cmd.exe shell, the current working directory is implicitly included when resolving the path of an executable. In shells such as bash or power shell that is not the case. It is possible to configure VSCode to support various shells (https://code.visualstudio.com/docs/terminal/shell-integration). However it might be a backward step to revert to CMD.exe just to save typing .\.

gcc main.c, compiles main.c and links the object code with the standard library and runtime using default build options. It is suitable only for simple single source file builds with no third-party libraries. Any non-trivial application will require more complex build options to compile and link multiple source files and third-party libraries, support debug, specify warning levels, control optimisation etc.

Normally for that you would use a makefile or similar build manager, or an IDE. For simple builds you could create a batch file, but your build is likely to outgrow that very soon. A proper build system will manage hundreds of source files and dependencies so that only code that ha changed or is dependent on something that has changed is rebuilt.

If I had to enter the long input every time I compiled it would be a nightmare!

That is more a question if how to operate a computer in general than it is about programming. A simple solution is to add the build command as a VSCode task as explained at https://devblogs.microsoft.com/cppblog/building-your-c-application-with-visual-studio-code/. That is similar to the batch file suggestion, except of course you can define a task that invokes a build manager such as make or cmake.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Clifford
  • 88,407
  • 13
  • 85
  • 165