Compiling on Linux using the Microchip C compiler

Getting your PIC code to compile in Linux used to be a hassle, but since the release of MPLAB-X, Microchip provides a fully functional Linux version of their C compiler. In this blog post, I'll walk you through it's installation so you can now easily compile Dwengo programs on the command-line. (If you want a graphical user interface, take a look at our MPLAB-X post).

Start by downloading the Microchip C compiler. You can find it on the MPLAB X download page External link: select the "Linux (32/64 bit)" platform, tick the box next to "MPLAB C18 Lite Compiler for PIC18 MCUs" and click "Download Now". If you're on the command-line of your Linux machine already, you can also try the command

wget http://www.microchip.com/mplabc18-linux-installer

You should now have a file called mplabc18-v3.40-linux-full-installer.run. First you'll need to make it executable:

chmod +x mplabc18-v3.40-linux-full-installer.run

Also, the compiler is a 32-bit executable so if you're on a 64-bit Linux machine you need the 32-bit libraries. On Debian/Ubuntu, you can install these by running

sudo apt-get install ia32-libs

Now you're ready to start the installation (as root):

sudo ./mplabc18-v3.40-linux-full-installer.run

If you accept the default settings, this will install the C compiler and all of its header files and libraries in /opt/microchip/mplabc18/v3.40

The compiler should now be fully functional, but first, let's make things a bit easier for ourselves. Create a file called /etc/profile.d/mcc.sh with the following content:

export MCC_HOME=/opt/microchip/mplabc18/v3.40
export MCC_BIN=${MCC_HOME}/bin
export MCC_INCLUDE=${MCC_HOME}/h
export MCC_LIB=${MCC_HOME}/lib
 
export PATH=$PATH:${MCC_BIN}

The next time you log in, several environment variables will be set up that allow the C compiler to be found more easily. The Makefile for the Dwengo library External link, for instance, looks at the MCC_* variables to determine where the compiler is installed. It is now very easy to download and compile the Dwengo library:

git clone https://github.com/dwengovzw/Dwengo-library.git External link
cd Dwengo-library/lib/
make

Similarly, if you write a Makefile for your own MCC project, use the MCC_* environment variables to locate the MCC compiler, include files and libraries:

  1. all: $(TARGET)
  2.  
  3. SOURCES=$(wildcard *.c)
  4. OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
  5. TARGET=project.hex
  6.  
  7. DWENGOLIB=~/Dwengo-library/lib
  8.  
  9. MCC_C=$(MCC_BIN)/mcc18
  10. MCC_L=$(MCC_BIN)/mplink
  11.  
  12. CFLAGS=-Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa- -p=18F4550 -i$(DWENGOLIB)
  13. LFLAGS=-q -u_CRUNTIME -p18F4550 -l$(MCC_LIB) -w
  14.  
  15. %.o: %.c
  16. $(MCC_C) $(CFLAGS) $< -fo=$@
  17.  
  18. $(TARGET): $(OBJECTS)
  19. $(MCC_L) $(LFLAGS) $< $(DWENGOLIB)/dwengo.lib -o$@

The next step is to program your Dwengo board from the Linux command line. Microchip also has a command-line tool for the PICkit2 programmer, which we talk about at this page.

Syndicate content