Setup Code::Blocks for STM8S Discovery

Unrestrained code generation for the STM8S Discovery board: Here's how to set up the free SDCC compiler and the Code::Blocks IDE (also free) under Windows.

I suggest reading the introduction STM8S Discovery – First contact with a cheapo first…

First of all we need the software. You will find Code::Blocks at; The SDCC compiler ist located at I'm using Win8, so I downloaded the 64 bit version sdcc-3.4.0-x64-setup.exe. Just do a full install with both applications.

Launch Code::Blocks. You will see a "Workspace" entry. It will very probably point to a directory called "CodeBlocks" in your personal "Documents" folder. This is where projects will be created, so remember that path.

Code::Blocks needs some adaptions in order to properly compile STM8 code. Open "Settings" > "Compiler" > "Global compiler settings". Choose "Small Device C Compiler" and add a new compiler flag for the "stm8" architecture setting:



Select the new CPU flag and the flag "Output Intel Hex" as default entry. You may also select some code optimization flags, but for the time being SDCC doesn't do much with them for STM8 code.


Switch to the tab "Toolchain executables" and choose the following settings:


Now go to "Other settings" > "Advanced options":


Adapt the options like this:


Finally save the compiler options dialogue.

Now let's create the project for our own STM8S105 static library.
Why a static library?
STM has created a "toolchain" called "Standard Peripheral Library". It consists of a number of header files (*.h) and C files (*.c). Most code examples make use of it by integrating the source code into each and every single project. This unnecessarily blows up the projects and makes them harder to understand. A static library can be compiled once and will be referenced only with the linker options of your projects. That's much smarter. Let's do it.

Create a new project with "File" > "New" > "Project…" > "Empty project". In the following popup, create a "Release" configuration for the static library and give it a recognizable name like "STM8S_lib".


The source code for this project is STM's "Standard Peripheral Library" (SPL). Go to and use their page search function to find "STSW-STM8069". Download the zip file and walk into "STM8S_StdPeriph_Lib_V2.1.0\Libraries\STM8S_StdPeriph_Driver". Copy the directories "inc" and "src" into the project folder; It's located inside your workspace folder.

According to STM the SPL code is solely meant for evaluation. That should't be a problem for us. But they hardcoded the three available commercial compilers into the source code; In addition SDCC uses some slightly different syntax constructs. Fortunately only three need adaption; You can download them here:

Just replace the three files in "src" and "inc".
Now choose "Project" > "Add files recursively", choose the project folder and make sure all .h and .c files are selected. You should then end up with something like this just below the "Workspace" entry:


OK, the source is fixed and integrated. Now some hard stuff: Project and target setup. Choose "Project" > Settings and modify these settings:


Under "build targetfiles" the C files for the STM8S105 need to be checked (and only these files). The following features are valid and their C file must be checked:
adc1, awu, beep, clk, exti, flash, gpio, i2c, itc, iwdg, rst, spi, tim1, tim2, tim3, tim4, uart2, wwdg.

From within the settings window click "Build options…". Make sure the "stm8" flag is checked for the SDCC compiler.
Choose the target ("STM8S_lib") and open the sub-tab "#defines", then add "STM8S105".


Don't close this window yet. Instead of the target check the project itself (root node) and go to "Search directories". Add the "inc" folder (you can add it as relative path when asked):


That's it! You should now be able to compile the selected source files and build the static library. Choose "Build" > "Build" and watch the magic.
Sometimes the SDCC library archiver "sdar.exe" crashes. In this case just build again until it succeeds.
Now open your explorer, go to the project directory and look for the folder "lib". If the resulting .lib file is there we're done.

You may now continue with a first program.


  1. ahmad


    I'm try to compile static library with your instraction but get this error:

        sdcc.exe  –opt-code-size -DSTM8S105  -mstm8   -Iinc  src\stm8s_adc1.c
         src\stm8s_adc1.c:687: error 74: function 'main' undefined

    I use :



           and win7 64bit

    please help me…




      It looks as though SDCC tries to create an executable, but you want to build a static library.
      Did you set the target option "Type: Static library"?

  2. Aaron


    The library compiles but it gives many warnings 'cast of LITERAL value to 'generic' pointer'  so in the file stm8s.h I found:

    #define ADC1_BaseAddress        0x53E0

    #define ADC1 ((ADC1_TypeDef *) ADC1_BaseAddress)

    Which is ILLEGAL, so I have 2 questions: 

    Do you have the same issue??

    How is it possible that it is just OK for the Raisonance and Cosmic compilers??

    could you advise?

    Best regards




  3. KodeIn


    I'm trying to replace IAR Embedded Workbench with Code::Blocks and SDCC.
    So I tried to build the libraries as you did, but I get a bunch of errors on stm8s_flash.c :

    ||=== Build: STM8S_lib in STM8S_lib (compiler: Small Device C Compiler) ===|
    src\stm8s_flash.c|170|syntax error: token -> 'Address' ; column 41|
    src\stm8s_flash.c|167|warning 85: in function FLASH_EraseByte unreferenced function argument : 'Address'|
    ——-[SNIP a whole lot of errors]——-
    ||=== Build failed: 10 error(s), 9 warning(s) (0 minute(s), 1 second(s)) ===|

    As I'm totally clueless as to why it doesn't work and since you figured out how to make it build in the first place…


    • Kenjutsu

      Have you been able to resolve the errors in stm8s_flash.c? I get the same errors using the Windows and OS X versions of Code::Blocks and SDCC.

  4. Jonas

    MAybe you can send me STM8S_StdPeriph_Lib_V2.1.0, exactly version 2.1.0, because it is not building with version 2.2.0 (has lot of errors)

  5. Jonas

    Yes STM8S_StdPeriph_Lib_V2.2.0, is not supported by SDCC,
    So could you give link to STM8S_StdPeriph_Lib_V2.1.0?

  6. Kenjutsu

    I am also getting errors with stm8s_flash.c:

    src/stm8s_flash.c|170|syntax error: token -> 'Address' ; column 41|

    src/stm8s_flash.c|167|warning 85: in function FLASH_EraseByte unreferenced function argument : 'Address'|

    src/stm8s_flash.c|185|syntax error: token -> 'Address' ; column 41|

    src/stm8s_flash.c|184|warning 85: in function FLASH_ProgramByte unreferenced function argument : 'Address'|

    src/stm8s_flash.c|184|warning 85: in function FLASH_ProgramByte unreferenced function argument : 'Data'|

    src/stm8s_flash.c|201|syntax error: token -> 'Address' ; column 49|

    src/stm8s_flash.c|198|warning 85: in function FLASH_ReadByte unreferenced function argument : 'Address'|

    src/stm8s_flash.c|222|syntax error: token -> 'Address' ; column 41|

    src/stm8s_flash.c|224|syntax error: token -> 'Address' ; column 42|

    src/stm8s_flash.c|226|syntax error: token -> 'Address' ; column 42|

    src/stm8s_flash.c|228|syntax error: token -> 'Address' ; column 42|

    Any ideas? 


    if i compile i get :

    ||=== Build: STM8S_lib in STM8_SPL (compiler: Small Device C Compiler) ===|
    inc/stm8s.h:81|3|error: #error "Unsupported Compiler!"|
    inc/stm8s.h:2637|25|fatal error: intrinsics.h: No such file or directory|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    !!! wher is pb !!!

  8. ars102

    Hi KodeIn,

    I got same error message and I have fixed it by modifying file stm8s.h inside

    In fact, that stm8s.h is pretty old and lacks of some new definitions. Go to line 146 and replaces lines

    #if defined (STM8S105) || defined (STM8S005) || defined (STM8S103) || defined (STM8S003) || \
        defined (STM8S903) || defined (STM8AF626x)
    /*!< Used with memory Models for code smaller than 64K */
     //#define PointerAttr NEAR
     #define PointerAttr NEAR

    #else /* STM8S208 or STM8S207 or STM8AF62Ax or STM8AF52Ax */
    /*!< Used with memory Models for code higher than 64K */
     #define PointerAttr FAR
    #endif /* STM8S105 or STM8S103 or STM8S003 or STM8S903 or STM8AF626x */




    #if defined (STM8S105) || defined (STM8S005) || defined (STM8S103) || defined (STM8S003) || \
        defined (STM8S903) || defined (STM8AF626x)
    /*!< Used with memory Models for code smaller than 64K */
     //#define PointerAttr NEAR
     #define PointerAttr NEAR
     #define MemoryAddressCast uint16_t

    #else /* STM8S208 or STM8S207 or STM8AF62Ax or STM8AF52Ax */
    /*!< Used with memory Models for code higher than 64K */
     #define PointerAttr FAR
     #define MemoryAddressCast uint32_t
    #endif /* STM8S105 or STM8S103 or STM8S003 or STM8S903 or STM8AF626x */

  9. JP

    I get the same error as the previous poster. Any solution?

Leave a Reply

Your email address will not be published.

Blue Captcha Image Refresh


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">