Notes About Working with Various Arduino & Netduino Microcontroller Boards

Friday, May 29, 2015

Building a Shared C Library Using gcc on the Arduino Galileo (Gen. 1)

I posted a couple days ago about installing the gcc compiler on an Arduino Galileo.  In the course of writing C code to use the Adafruit BMP085 barometric pressure sensor, I found this smbus code (in  a post about I2C on the Raspberry Pi) that works well with I2C devices connected to the Galileo without any modification.  I'll have other projects that use I2C, so I wanted to build smbus as a shared library.  Below are my brief notes on the process of using gcc to build the library.

The steps below assume that you are working at the Galileo's command prompt (not the Arduino IDE).

To build the shared library:

  1. gcc -c -Wall -fpic smbus.c   (create smbus.o object file in PIC format, show all warnings)
  2. gcc -shared -o libsmbus.so smbus.o (create shared library .so file. Name of .so file must start with lib)
Put the library where gcc can find it:
  1. cp libsmbus.so /usr/lib/
  2. chmod 0755 /usr/lib/libsmbus.so
  3. ldconfig
  4. cp smbus.h /usr/include
To use this library with gcc, include -lsmbus in the gcc commmand line, like this -

gcc -Wall -lm -lsmbus -o bmp085 bmp085.c 

Note how the library name after -l matches the name of the library file minus the lib prefix and the .so file extension. -lm is the switch for including the standard math library that my program uses.

I'll post my C code for reading barometric pressure using the BMP085 separately.


No comments:

Post a Comment