A static library for piControlIf
First a new project piControlIf is created for a static library. Then the following files are added
Since I didn’t change anything in the files there shouldn’t be much to debug. Since I want to create a library that can be used productively, I change the configuration to Release by right-clicking on Project, Set Configuration → Release.
After building the project, the files are all located in the project directory on the RevPi. I would like to make the library and the corresponding header files globally available. As described in the previous article, the directories in /usr/local are provided for this purpose. /usr/local/lib for the libraries and /usr/local/include for the header file. So the files are copied as follows:
sudo cp lilcontrolif.a /usr/local/lib/ sudo cp piControl.h /usr/local/include/ sudo cp piControlIf.hpp /usr/local/include/
A test program for the library
Now quickly create a test program. For this I simply used RevPiBlink from one of the previous articles and made RevPiBlinkStaticLib out of it. Remove the unnecessary files piControlIf.cpp, piControlIf.hpp, piControl.h, which are already available on the RevPi. In the include statement for piControlIf.hpp the quotes must now be replaced by <>, so that the compiler no longer searches for the files in the project directory:
Last but not least the linker has to be told that libpicontrolif.a should be included. As described in the last article this is done via Project Properties → Build → Linker → Libraries → Add Library. Alternatively, you can also use Project Properties → Build → Linker and enter -lpicontrolif in Additional Options.
The parameter -l<library> tells the linker to include the corresponding library. It is important to include the library without the preceding lib or the file extension .so. If the project is executed, A1 flashes every second.
A dynamic library for piControlIf
Why always compile the piControl library permanently into a program. It makes much more sense to make a dynamic library out of it. For this I use the existing library project “piControlIf”. To create a dynamic library instead of a static one, simply select Project Properties → Build → Configuration Type → Dynamic Library.
When the library is created, you will find the file libpiControlIf.so in the output directory. What happens in the background? The project is compiled with the parameters -fpic or -fPIC. PIC stands for “Position independet Code”. This is a prerequisite for a dynamic library. -fPIC (capitalised) is only relevant for special architectures. On X86 systems it makes no difference according to man page. ARM systems do not seem to be affected either. With -shared the linker gets the instruction to create a .so file.
Wisely, Netbeans creates the file this time in upper and lower case while static libraries were all lower case. This avoids naming conflicts when there is an equivalent static and dynamic library on the system.
As with the static library, the files must now be copied to /usr/local/lib. Since the header files already exist, only the library file must be copied:
sudo cp libpiControlIf.so /usr/local/lib
A dynamic library must be found by the dynamic linker at runtime. In contrast to the static linker, the dynamic linker does not search through all files in the corresponding directories, but consults a table. This table is updated with ldconfig.
With ldconfig -p you can check if the library was found correctly.
ldconfig -p | grep libpiControlIf
Now we need a test program. Again I copied RevPiBlinkStaticLib and called it RevPiBlinkDynamicLib. In Project Properties → Build → Linker → Libraries the static library picontrolif must be replaced by the dynamic piControlIf.
With ldd you can see which dependencies our program has