Table of Contents

Programming

bash

C

Makefiles

.PHONY : clean
clean :
           -rm edit $(objects)
vpath %.h ../headers
all : prog1 prog2 prog3
     .PHONY : all
... rules for programs
   %.d: %.c
           @set -e; rm -f $@; \
            $(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \
            sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
            rm -f $@.$$$$
   subsystem:
           cd subdir && $(MAKE)
define name
cmd
cmd
...
endef
foo := a.o b.o c.o
bar := $(foo:.o=.c)
ifeq(variable, value)
    statements
else
    statements
endif

Microcontrollers and GCC

Programming and generating code for a system where Linux is already running usually involves just writing the source code for the program, some libraries and some Makefiles. One thing that is always happening, every time that some code is generated that can be useful to use in a system, is linking. During linking basically the code generated by the compiling process, called object code, from all the source files plus the code from library files gets “linked” in one single output object code in a specific format. This linking process is necessary because the operating system needs the information of how and where to look for code from other “shared libraries”, also how to put the code generated in memory and also how to execute this code. Something I didn't know is that this information on how linking should happen is specified in two ways. First with specific standards and object file types and, second with instructions given to the linker using a linker script file. The approach that I follow to deal with this was the following:

  1. Get an example of a project for a microcontroller that requires to set memory layouts by hand, I got an example for the stm32 Arm microcontrollers.
  2. Read the complete GCC users manual. This is important to know what is going on during the whole compilation process.
  3. Read the complete Makefile users manual. This is big but very important. Now I understand much more variations of Makefiles.
  4. Read the complete manual for ld the linker. This is where you will find the information for writing linker scripts.

You may wander why you don't have to do this in your linux i386 PC computer, this is because there is already a default ld script for your system and it gets used automatically. So you don't notice. But if you dare you can try to write your own ld script for your i386 and try to get your program running with it.