Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Friday 13 November 2026 @ 17:29:40
BeagleBone Black · Revision D

libioctrl

Bring together the GPIO, ADC and PWM libraries you have developed into one reusable C library. Build static and shared versions, install them system-wide, and migrate your existing applications to use libioctrl.

Embedded LinuxC librariesGPIOADCPWMMake
Practical Checklist
Chapter checklist 0 / 18 completed

1. Consolidate the source code

Rather than maintaining three independent libraries, we will centralise their implementation files in src/, their public headers in include/, and provide a single umbrella header, libioctrl.h.

  1. Create the new project directory and its src and include subdirectories.

    $ mkdir -p ~/libioctrl/{src, include}
  2. Copy the existing ADC source and header into their respective directories.

    $ cp bbb_adc/bbb_adc.c ~/libioctrl/src && cp bbb_adc/bbb_adc.h ~/libioctrl/include

    Repeat the same process for bbb_gpio.{c,h} and bbb_pwm.{c,h}.

  3. Inspect the resulting project structure.

    $ ls -l ~/libioctrl/*
    libioctrl/include:
    total 12
    -rw-r--r-- 1 debian debian  685 Apr 14 19:25 bbb_adc.h
    -rw-r--r-- 1 debian debian 2557 Apr 14 20:35 bbb_gpio.h
    -rw-r--r-- 1 debian debian 1636 Apr 14 20:35 bbb_pwm.h
    
    libioctrl/src:
    total 12
    -rw-r--r-- 1 debian debian 1288 Apr 14 19:24 bbb_adc.c
    -rw-r--r-- 1 debian debian 3559 Apr 14 19:46 bbb_gpio.c
    -rw-r--r-- 1 debian debian 2490 Apr 14 19:24 bbb_pwm.c
  1. Create include/libioctrl.h and reproduce the following umbrella header.

    #ifndef LIBIOCTRL_H
    #define LIBIOCTRL_H
    
    #include "bbb_gpio.h"
    #include "bbb_pwm.h"
    #include "bbb_adc.h"
    
    #endif // LIBIOCTRL_H
  2. Update the header inclusion in each of the three implementation files. The supplied example for src/bbb_adc.c is:

    # include "../include/bbb_adc.h"

    Repeat the same pattern for the other *.c files.

2. Build and install with Make

The Makefile compiles each C source into an object file, combines those objects into static and shared libraries, and installs the resulting public interface.

  1. Before installing libioctrl, uninstall and clean the three previously installed libraries using their existing Makefiles.

    Repeat the original commands in each old library directory:

    make uninstall
    make clean

  2. Create a Makefile in ~/libioctrl and reproduce the supplied build rules.

    CC = gcc
    CFLAGS = -Wall -Werror -fPIC -I./include  # Include headers
    AR = ar
    ARFLAGS = rcs
    PREFIX = /usr
    INCLUDEDIR = $(PREFIX)/include
    LIBDIR = $(PREFIX)/lib
    
    # Directories
    SRCDIR = ./src
    OBJDIR = ./obj
    
    # Library names
    TARGET_STATIC = libioctrl.a
    TARGET_SHARED = libioctrl.so
    
    # Source files for each component
    SRC = $(SRCDIR)/bbb_gpio.c $(SRCDIR)/bbb_pwm.c $(SRCDIR)/bbb_adc.c
    OBJ = $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRC))
    
    # Default target: Build both libraries
    all: $(TARGET_STATIC) $(TARGET_SHARED)
    
    # Compile source files into object files
    $(OBJDIR)/%.o: $(SRCDIR)/%.c
            @mkdir -p $(OBJDIR)
            $(CC) $(CFLAGS) -c $< -o $@
    
    # Create the static library
    $(TARGET_STATIC): $(OBJ)
            $(AR) $(ARFLAGS) $@ $(OBJ)
    
    # Create the shared library
    $(TARGET_SHARED): $(OBJ)
            $(CC) -shared -o $@ $(OBJ)
    
    # Clean up build artifacts
    clean:
            rm -rf $(OBJDIR) $(TARGET_STATIC) $(TARGET_SHARED)
    
    # Install libraries and headers
    install: $(TARGET_STATIC) $(TARGET_SHARED)
            sudo cp include/bbb_gpio.h include/bbb_pwm.h include/bbb_adc.h include/bbb_libioctrl.h $(INCLUDEDIR)/
            sudo cp $(TARGET_STATIC) $(TARGET_SHARED) $(LIBDIR)/
            sudo ldconfig
    
    # Uninstall libraries and headers
    uninstall:
            sudo rm -f $(INCLUDEDIR)/bbb_gpio.h $(INCLUDEDIR)/bbb_pwm.h $(INCLUDEDIR)/bbb_adc.h $(INCLUDEDIR)/libioctrl.h
            sudo rm -f $(LIBDIR)/$(TARGET_STATIC) $(LIBDIR)/$(TARGET_SHARED) $(OBJDIR)
            sudo ldconfig
    
    # Phony targets
    .PHONY: all clean install uninstall
  1. Run the original installation command from the libioctrl directory.

    make install

  2. Inspect what make install is doing at each stage.

3. Use the unified library

Once installed, applications can use the combined public header and link against libioctrl rather than the individual libraries.

  1. Choose one of your earlier C examples: adc2pwm.c, adc_continous_read, adc_read, blink or pwm_test.

    For example, update the header include in adc_read.c:

    #include <libioctrl.h> # originally bbb_adc.h
    #include <stdio.h>
    #include <unistd.h>
    ...
  2. Change the corresponding Makefile library link option.

    ...
    # Library to link against
    LIBS = -lioctrl # originally, lbbb_adc
    ...

    Repeat for other applications as appropriate, and remember to rebuild each executable with make clean followed by make.

  3. Compile and run your updated example. Confirm its behaviour is unchanged now that it links against the unified library.

4. Alternative: Fork and clone libioctrl

The repository provides an alternative copy of the library and completed C examples, which can be transferred to the BeagleBone over the USB network connection.

  1. Fork the repository from GitHub on your computer:

    CompEng0001/bb.black_pin_library

  2. Clone your fork on the computer:

    git clone git@github.com:YOURUSERNAME/bb.black_pin_library
  3. From the parent directory containing the clone, transfer it to the BeagleBone with the supplied scp command:

    scp -r /path/to/bb.black_pin_library/ debian@192.168.7.2:libioctrl

  1. In the transferred repository, run its installation script:

    bash install.sh

  2. Explore the supplied examples_C directory and compare the implementations with your own programmes.