This week I’ve been fighting with my PIC18F4550 for my TFT LCD abd SD card reader controllers.  I’ve been using the HiTech C before and since a couple of months XC8 from Microchip, but now I encountered some very strange behavior.  The timing of the SD card access was not right according my logic sniffer, thus I started to dig deeper for the cause.  I figured it out quite quickly (the XC8 compilers warns about this for the free version): the compiler embedded some appalling bogus code to slow down the software to promote the pro version.  Thus I checked for the pro version it is priced at 995 USD, which is quite high for a hobbyist like me.


After some research I stumbled upon SDCC, an open source, retargetable and optimizing C compiler for a whole bunch of microcontrollers.  And my PIC16 and PIC18 series are very well supported.

I’ve been playing now for a little while with this compiler and I’m very pleased with the code it generates.  Although my assembler is a bit rusty, I can say that I couldn’t have done a better job myself in writing the assembler code myself.  The code generated by sdcc is squeaky clean and very efficient.


I want to keep the MPLAB X environment as the IDE is NetBeans based and pretty comfortable to work with.  Thus I had to install the sdcc toolchain in MPLAB X.  It needs 2 software bundles to be downloaded and a plugin for MPLAB X.  The software bundles are SDCC itself and GPUTILS.  GPUTILS(GNU PIC Utilities) is a collection of tools for the PIC microcontrollers which include gpasm, gplink and gplib.

Just install SDCC and GPUTILS and make sure that your they are accessible from within the path (I’m using Windows).  By default this should not be problem as both installers do a pretty good job.

Now we need to tell MPLAB X that a new toolchain is available, but tough luck, MPLAB X didn’t recognize the SDCC toolchain.  After some googling and fiddling with MPLAB X, I discovered that MPLAB X needs an extra plugin.  Simply go to Tools – Plugins – Available Plugins and add ‘SDCC Toolchain’.  And voilà… the SDCC toolchain is available when you define a new project.

One weird ‘feature’ of the compiler is that the headers for the PIC microcontrollers are located under the ‘non-free’ directory and that you have enable this directory in the toolchain: right click your project – Properties – SDCC Toolchain – sdcc ; in the general option categories, check the ‘Use non free libraries/header.

Also at compilation I got some library errors which can be fixed by adding some additional options for sdcc: right click your project – Properties – SDCC Toolchain – sdcc – Additional options.  Put the following (remember, I’m running Windows and the gnu tools don’t like spaces):

-LC:\Progra~1\SDCC\lib\pic16 -IC:\Progra~1\SDCC\include -IProgra~1\SDCC\non-free\include\pic16 -IC:\Progra~1\SDCC\include\pic16  libc18f.lib


1. I’m still having some problems with the global library includes (like stdio.h for instance) in the MPLAP X editor.  MPLAB X claims they don’t exist, although they seem to work flawless.  I’ll investigate somewhat later to solve this.  For now it’s only a minor annoyance.

2. Inline declarations are not allowed, for instance

for (unsigned char i = 0; i < 8; i++) {

is not allowed, it generates a compilation error and must be replaced with:

unsigned char i;
for (i = 0; i < 8; i++) {

But this not a major inconvenience, on the contrary it will force myself to put my declarations at the top of each function.

3. The inline assembly syntax is quite different from XC8, example:

__asm__("movlb (_delayus_variable)");

Notice the \n between the different assembler instructions..


A very good compiler and it is free!  The people behind this project did a tremendous good job!  I was astonished by the quality of the generated code for PIC.  It’s not only fast but also very clean.  Being a software developer myself (mainly Java), a quality that I would like to see in all my projects.

I’ll now keep myself busy in porting my existing C libraries for PIC to the SDCC toolchain.  The required work is very minimal as luckily there are almost no syntactical differences between XC8 and SDCC.

Tagged . Bookmark the permalink.

14 Responses to SDCC for PIC with MPLAB X

  1. Jim says:

    Hi Patrick, thanks for this very useful post, I am hoping to try SDCC for PIC soon. I did not like the look of XC8 free version … heard bad things about it.

    • pleyman says:

      Well indeed, I didn’t like the fact that they mess with your code. If they want to promote a paying version, fine by me, limit the code size or so. But please keep your hands of the generated code, it’s causing nothing but trouble.

  2. caldran says:

    Thanks for the guide. Did you find a solution for problem with global library includes?

    • pleyman says:

      You’re welcome, glad you liked it…
      As for the libraries, I didn’t really investigate for the deeper cause of the IDE not recognizing the global libs. It’s only when editing the code that the auto complete doesn’t seem to work, also the IDE thinks that the code is erroneous; but the code compiles without any problem. I think it is just a matter on configuring the correct settings in MPLAB X, but still need to figure out which ones.

  3. rlsrtand says:

    Hi Patrick, I have tried this with MPLABX 2.10 and I can see the compiler in tools->options->embedded. It is set as default. When I create a new project the SDCC compiler is not shown as a toolchain selection. Any idea what I did wrong?

    • pleyman says:

      Hmm, since the toolchain seems to be recognized by MPLAB, I’m thinking that the choice of the device when you start a project is disabling the SDCC toolchain. Not all devices are supported by SDCC. To my knowledge almost all PIC16 and quite some PIC18 are supported.

      • AaronD says:

        I think you’re right. I made a new project and found that SDCC is not available for me either, despite showing correctly under Tools->Options. I changed the chip selection from my relatively new one (which IS in SDCC’s supported list) to an older one, and it’s suddenly available.

  4. akbar patil says:

    Dear Sir,
    I have mplab x v 2.35 , gputils 1.4.0, sdcc – 3.4.0 x64
    laptop sony vaio core i5. when itried to copile code following message coming pl help me.

    CLEAN SUCCESSFUL (total time: 58ms)
    make -f nbproject/ SUBPROJECTS= .build-conf
    make[1]: Entering directory ‘C:/Users/akbar/MPLABXProjects/led20.X’
    make -f nbproject/ dist/default/production/led20.X.production.hex
    make[2]: Entering directory ‘C:/Users/akbar/MPLABXProjects/led20.X’
    gnumkdir -p build/default/production
    rm -f build/default/production/newmain.o
    “C:\Program Files\SDCC\bin\sdcc.exe” –debug-ralloc –use-non-free –pstack-model=large -LC:\Progra~1\SDCC\lib\pic16 -IC:\Progra~1\SDCC\include -IProgra~1\SDCC\non-free\include\pic16 -IC:\Progra~1\SDCC\include\pic16 libc18f.lib -c -mpic16 -p18f2525 newmain.c -obuild/default/production/newmain.o
    Preproc file not found: No error
    make[2]: *** [build/default/production/newmain.o] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    nbproject/ recipe for target ‘build/default/production/newmain.o’ failed
    make[2]: Leaving directory ‘C:/Users/akbar/MPLABXProjects/led20.X’
    nbproject/ recipe for target ‘.build-conf’ failed
    make[1]: Leaving directory ‘C:/Users/akbar/MPLABXProjects/led20.X’
    nbproject/ recipe for target ‘.build-impl’ failed

    BUILD FAILED (exit value 2, total time: 462ms)

    • Patrick says:

      I haven’t encountered this error myself. But since the error is produced by ‘make’, I would guess that one of the required libraries to run your code was not found by the make command.

  5. akbar patil says:

    how rectify it please

  6. majid says:

    may you explain how to link SDCC in MPLAB X?
    it’s not SDCC plug in “Available Plugins”?
    i install both SDCC and GPUTILS?
    help me

    • Patrick says:

      Make sure you have installed the ‘SDCC Toolchain’ plugin via Tools – Plugins – Available Plugins. If you can’t see the plugin verify if in the Settings tab of the Plugins window all three update centers are active.

  7. Jim Dunne says:

    I’m in a similar situation trying to use SDCC instead of C18 or XC8 since I can’t afford the steep $995 license cost as a hobbyist. I’ve done a manual conversion of C18 code to SDCC for my project but am having issues with linking.

    I’m running SDCC 3.6.0 and GPUTILS 1.5.0 (latest as of this writing 2016-12-13). The SDCC compiler works fine and generates .o files (not .rel files like the docs say) but GPLINK always fails with an assertion error:

    Assertion failed!

    Program: C:\PROGRA~2\gputils\bin\gplink.exe
    File: gpcofflink.c, Line 190

    Expression: sym != NULL

    Have you run into this issue? It seems no matter what I try I get this exact error.

  8. Muhammad says:

    Hi Patrick, Very helpful article, though I have a problem while debugging my program as watches list doesn’t show any variables while debugging and once i stop the debugger all variables appear again. I tried to export those variables while not debugging and importing them again while debugging, but didn’t work also. Have you any thoughts or hints?
    I am using mplab x v4.05 with sdcc v3.5.0 via simulator
    Appreciate it. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *