A third example using external libraries

With this example I am getting closer to my project structure.

Understanding this step should help in future projects to use existing libraries located anywhere in the accessible directories, for instance existing Arduino libraries, without the need to copy them.

I will use an implementation of calculating Fibonacci numbers within limits.

The code and the libraries are somewhere in my home directory.

Here exists a directory „learning_cmake“ and within all projects and libraries are placed:

This shows the evolution of the learning process from a simple single file approach, split to several files in sub-directories till using a common library path.

Her are the details about the common library with that fibonacci function.

The „common_libraries/fibonacci/CMakeLists.txt“ file:

The header file „common_libraries/fibonacci/fibo.h“:

The sorce code „common_libraries/fibonacci/fibo.c“:

This example uses a copy of the previous example.

Only the „CMakeLists.txt“ and the „main.c“ files needs to be modified.

Here are these modified files.

The file „step1_c_ansi_c/CMakeLists.txt“:

„cmake“ reads the files in a linear order.
This implies some commands must be in a certain order.
Right after setting the „C“ standard we add the reference to the external library.
The „EXTERNAL_LIB_DIR“ references
„$ENV{HOME}/learning_cmake/my_cmake_code_examples/common_libraries“.
The first part „$ENV{HOME}“ expands to „/var/home/kps“ which is my home directory.

The complete reference looks like this:
„/var/home/kps/learning_cmake/my_cmake_code_examples/common_libraries“

The same approach may be used when using libraries from the Arduino world, because they are placed in a well known way.

In my case:

„/var/home/kps/programming/arduino_libraries/libraries/“

But this will be used in one of the future examples when using these libraries without the Arduino IDE.

After specifying this path it can be used in „set()“ command to create a variable „FIBO_LIB_PATH“.

As always in programming it is necessary to check if a value is correct or an item exists.

After all this we add the internal libraries one by one and tell the linker to include them.

Then we tell the linker to use the external library, too.

That’s it for the root „CMakeLists.txt“ file.
It should be possible to use the function „print_fibonacci_sequence()“ anywhere in the „main.c“ file.

The up to now complete „step1_c_ansi_c/main.c“:

Here the function is used as the last action in the „main.c“ file.

And when executing it with „$ build/sqrt_test 16“ the output should show:

I am now ready to move away from native compiling with „C“and the standard Linux compiler towards cross compiling using the RP2xxx processors on Pico boards using the the RP2xxx Software Development Kit (SDK) with it’s libraries.

But this is something for the not so far away future,

Stay tuned!