Quadcopter part 7: C++ Programming with BlackLib

[UPDATE]: this was on kernel version 3.8.  I’ve started over on kernel version 4.4, and since the PWM code in BlackLib (v2 and v3) aren’t working with 4.4, I’ve decided to do the implementation myself.  All the C++11 options, dialects, etc can be removed from the eclipse project settings.
BlackLib v3 does have a nice Makefile now, so building and using it doesn’t require you to set up a seperate project in eclipse anymore, you just need to specify the include path and the library path.

First you’ll need the cross compiler. For example, on Ubuntu:
sudo apt-get install gcc-arm-linux-gnueabihf

This will give you:
nicky@nicky-Precision-M4800:~/Projects/BlackLib$ arm-linux-gnueabihf-g++ --version
arm-linux-gnueabihf-g++ (Ubuntu/Linaro 4.9.1-16ubuntu6) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

nicky@nicky-Precision-M4800:~/Projects/BlackLib$ arm-linux-gnueabihf-gcc –version
arm-linux-gnueabihf-gcc (Ubuntu/Linaro 4.9.1-16ubuntu6) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

However it seems that this version is a bit more recent then the one BlackLib has been developed with, and it won’t compile due to a few missing includes. You can find this fix on my github repo: https://github.com/panic1/BlackLib/tree/unistd

Now I will be using eclipse to build a shared library from this BlackLib code, in my setup I’m using Eclipse Luna with CDT.

  1. Create a new C++ Project, Click next.
  2. Project name: BlackLib
  3. Uncheck default location, and select the location where you checked out the BlackLib code
  4. Select Shared Library, Empty Project
  5. Select cross toolchain (you might need to uncheck the checkmark at the bottom to show all toolchain options), and click next
  6. The toolchain prefix: arm-linux-gnueabihf-. Click finish

Now the fun starts, BlackLib is written with quite advanced C++11 functionality, and the compiler will have to be enable to allow these experimental features:

  1. Right click on the project, select Properties
  2. Go to C/C++ Build, Settings
  3. In the Cross Settings the prefix should be set to arm-linux-gnueabihf- (just in case you’ve forgotten in a previous step
  4. In the Cross C++ Compiler, Dialect, Language standard should be set to ISO C++11
  5. In the Cross C++ Compiler, Preprocessor, add a new symbol __GXX_EXPERIMENTAL_CXX0X__
  6. In the Cross C++ Compiler, Miscellaneous, check the Position Independent Code (-fPIC)
  7. Apply

As we don’t need them in the resulting library, in the project you can right click, go to Resource Configuration, and Exclude from Build the following folders:

  • SPI_SETUP
  • V1_0
  • V2_0/SPI_SETUP
  • V2_0/exampleAndTiming
  • exampleAndTiming.cpp

Now go ahead and build the project, this should result in a libBlackLib.so file we’ll start to use in the next steps.

Using the library in a new project is pretty straight forward:

Create a new C++ project in Eclipse, choose Empty Project and the Cross GCC toolchain, and click next. The toolchain prefix should be arm-linux-gnueabihf-, just like before.

Right click the project, and go to Properties, Project References and select the BlackLib project to be referenced.
Go to C/C++ Build, Settings, and add the path to the BlackLib/v2.0 folder Cross G++ Compiler Includes, and Cross G++ Linker Libraries should include the BlackLib library, and the path to the BlackLib/Debug where the libBlackLib.so file can be found.

To run your application on the Beaglebone board, you will first have to copy the libBlackLib.so file. There are several valid locations where Linux searches for shared libraries, I usually put it in /usr/lib/. Then copy your application binary, location doesn’t really matter, for example /root or /home/debian, make the binary executable (chmod), and run it.

3 Replies to “Quadcopter part 7: C++ Programming with BlackLib”

  1. Hello,
    Your project is awsome.
    I am tring to test GPIOs of Beaglebone; I have compiled my program with the library succesfully in Eclipse IDE but after I upload the binary and run the program; Beaglebone throws this error:
    error while loading shared libraries: libBlackLib.so: cannot open shared object.

    What is missing, in the Beaglebone??

  2. Hi, Joel. Thank you for your interest. It seems I forgot to mention that you need to copy the libBlackLib.so file to your beaglebone as well. There are several valid locations where Linux will search for these shared libraries by default, I usually put it in the /usr/lib directory. I will update the article.

  3. For more advanced use, you could also go for a static library. In eclipse it’s quite easy to change the lib project into a static library. This will produce a libBlackLib.a file. If you then setup your application’s project to include this static library in the linking step, the entire blacklib code will be linked into your application binary and you won’t need to copy the library file to the beaglebone. Disadvantages are that the binary grows in size and that you will need to respect any GPL license that is on that library code, advantage is that now your application will start up faster and you can’t forget to update your library files when you change something in that code.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.