User forums > Using Code::Blocks

SDL2 with CodeBlocks 20.03

(1/2) > >>

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 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)?

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 but decided to stop when something else happened in my life.

Tim S.

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?

Post a full rebuild log so I can see the include folders that are being used.

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

Tim S.

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.


[0] Message Index

[#] Next page

Go to full version