Author Topic: SDL2 with CodeBlocks 20.03  (Read 10054 times)

Offline omnishambles

  • Multiple posting newcomer
  • *
  • Posts: 12
SDL2 with CodeBlocks 20.03
« on: July 02, 2020, 08:21:55 pm »
I'm on Windows 10 64-bit and have installed CodeBlocks (which is my first IDE) with MinGW/gcc. I'm ok with C but currently know very little about dev admin (linker settings, libraries, headers, flags/switches, etc.).

I want to use SDL2 in C with CodeBlocks 20.03, but want to set it up right first time rather than get stressed hacking about. As 20.03 is quite new and stable releases are infrequent, all the guides/tutorials I found are for 17.12 or earlier (e.g. Lazy Foo, the wiki and forum here). As it's been 3 years I guess a lot of things have changed, so would appreciate some up to date help rather than risk getting lost with an out of date guide. When googling I see a lot of people saying the wiki or the SDL wizard is out of date and you have to set SDL up manually, and that's even pre-20.03.

I downloaded the MinGW development library from libsdl.org: SDL2-devel-2.0.12-mingw.tar.gz (MinGW 32/64-bit). What would be a good way forward from here - follow Lazy Foo (written for CodeBlocks 12.11)?

Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: SDL2 with CodeBlocks 20.03
« Reply #1 on: July 03, 2020, 01:29:59 am »
1. Figure out what compiler you are using? You stated GCC
1b. Figure out if the GCC is 64 bit or 32 bit?
     I choose to use an 64 bit MinGW GCC.

2. Set the value of the SDL2 Global Variable
   I used the base value of "C:\Users\stahta01\devel\libraries\SDL2-2.0.12\x86_64-w64-mingw32"
       without the double quotes. Please choose a path without spaces and using only ASCII characters.

3. Run the wizard; note, I have you set the Global Variable first because it avoids a Wizard warning/error.

If you have questions about a step ask them!

Edit2: Before you try to use someone directions; it is a good idea just to get the wizard code to work.
This way you know the Library and the Compiler are working okay.

Most people use "Lazy Foo"; but, I did not like the license.
I tried to use https://www.willusher.io/pages/sdl2/ but decided to stop when something else happened in my life.

Tim S.

« Last Edit: July 03, 2020, 01:51:03 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline omnishambles

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: SDL2 with CodeBlocks 20.03
« Reply #2 on: July 03, 2020, 02:02:50 pm »
I'm writing this with other readers in mind (including future me) so my answers are a bit verbose. '...' denotes irrelevant directories.

1. 64-bit. From the CB site I installed codeblocks-20.03mingw-setup.exe. C:\...\codeblocks\MinGW\bin\gcc.exe -v says Target: x86_64-w64-mingw32

2. In Settings / Global Variables I created the variable 'SDL2' and set its base value to C:\...\x86_64-w64-mingw32. (CB changed the variable to lowercase sdl2)

3. Did File / New / Project / SDL2 project / project name = codeblocks_sdltest1. It asked where SDL2 is installed and offered $(#sdl2) which I accepted as-is. Accepted the compiler/build defaults. It created a C++ project with a main.cpp, but I need C. The wizard didn't give a C/C++ choice.

Looking at main.cpp, the relevant parts for a skeleton C equivalent seem to be:
#include <SDL.h>
int main( int argc, char * argv[] )

So I created a new project (console application, C) with main.c:
#include <stdio.h>
#include <SDL.h>
int main(int argc,char *argv[]) {
    printf("Hello world!\n");
    return 0;
}

Which failed to build with:
C:\...\codeblocks_sdltest2\main.c|2|fatal error: SDL.h: No such file or directory|

The following all failed in the same way:
#include <SDL2/SDL.h>
#include <SDL2\SDL.h>
#include <include/SDL2/SDL.h>
#include <include\SDL2\SDL.h>

SDL.h exists, at C:\...\x86_64-w64-mingw32\include\SDL2\SDL.h
The global variable sdl2 is still set to C:\...\x86_64-w64-mingw32

If I do any more I'll just be guessing and hacking which is what I wanted to avoid, so I'm stuck at this point. Any ideas?

Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: SDL2 with CodeBlocks 20.03
« Reply #3 on: July 03, 2020, 02:07:41 pm »
Post a full rebuild log so I can see the include folders that are being used.
http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Edit: The above is useful; but, too early till you try to add the includes needed.

Tim S.
« Last Edit: July 03, 2020, 02:14:19 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: SDL2 with CodeBlocks 20.03
« Reply #4 on: July 03, 2020, 02:12:04 pm »
If you do not wish to use the Wizard than you need to add all the search paths both linker and include.
And, you need to add the defines and libraries.

I think that rename the main.cpp to main.c is easier.
Save as main.cpp to new name of  main.c
Add main.c to project.
Remove main.cpp from project.

If you wish to not use the Wizard for an SDL2 project; I would instead use it for an blank CB project and add everything by hand. I am will to help you add the needed things to a blank/empty CB project.

Tim S.
« Last Edit: July 03, 2020, 02:15:20 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline omnishambles

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: SDL2 with CodeBlocks 20.03
« Reply #5 on: July 03, 2020, 02:54:05 pm »
Thanks for your help. In answer to your reply #3, I did a rebuild and the build log is as follows. (I just noticed your edit to reply #3 but am posting this anyway - I'll respond to reply #4 later).

-------------- Clean: Debug in codeblocks_sdltest2 (compiler: GNU GCC Compiler)---------------

Cleaned "codeblocks_sdltest2 - Debug"

-------------- Build: Debug in codeblocks_sdltest2 (compiler: GNU GCC Compiler)---------------

gcc.exe -Wall -g  -c C:\data\source_code\c\codeblocks_sdltest2\main.c -o obj\Debug\main.o
gcc.exe  -o bin\Debug\codeblocks_sdltest2.exe obj\Debug\main.o   
C:\data\source_code\c\codeblocks_sdltest2\main.c:2:10: fatal error: SDL.h: No such file or directory
 #include <SDL.h>
          ^~~~~~~
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Offline omnishambles

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: SDL2 with CodeBlocks 20.03
« Reply #6 on: July 03, 2020, 03:08:19 pm »
Re reply#4, I definitely would want to use the wizard instead of setting things up manually as you describe. That's exactly what I want to avoid if at all possible. However, if the wizard doesn't support C there may be no choice. It sounds like it's going to be a long and winding road so I'll have to leave it until the weekend. Thanks for offering to help.

Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: SDL2 with CodeBlocks 20.03
« Reply #7 on: July 03, 2020, 03:16:47 pm »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Online stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: SDL2 with CodeBlocks 20.03
« Reply #8 on: July 03, 2020, 03:28:56 pm »
Most people use "Lazy Foo"; but, I did not like the license.
I tried to use https://www.willusher.io/pages/sdl2/ but decided to stop when something else happened in my life.

Since "Lazy Foo" uses C and www.willusher.io uses C++ you likely will want to use "Lazy Foo".
But, if this is for commercial use you likely want to find another place to learn SDL2!
Unless the license for "Lazy Foo" has changed since 5 years, ago.

Tim S.


C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline omnishambles

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: SDL2 with CodeBlocks 20.03
« Reply #9 on: July 03, 2020, 03:35:01 pm »
Quick reply - your project worked perfectly on my machine. Thanks!

I don't understand how CB works yet, but clearly some settings relating to libraries, search paths, etc. are set in the project file as opposed to CB's settings menus. Per-project settings as opposed to global CB settings I guess. Haven't got time to look closely now, but I'm very pleased that it works at all, and will look at this over the weekend. Many thanks again.

On the licence issue, I see what you mean about Lazy Foo. I use tutorials to learn principles from rather than copy/paste from, so that shouldn't be a problem. I'll use the libsdl docs as a starting point anyway.