Compiling and Linking Against a Library

The following is an example of using the library with a GCC compiler. The library is used by including the header file matrix.h and then calling the necessary library functions.

/* main.cpp (pseudocode) */
#include "matrix.h"
int main(int argc, char* argv[])
  float *A, *B, *C, *D;
  float *J, *K, *L;
  float *X, *Y, *Z;
  mmultadd(A, B, C, D);
  mmult(J, K, L);
  madd(X, Y, Z);

To compile against a library, the compiler needs the header file. The path to the header file is specified using the -I switch. You can find example files in the samples/libmatrix/use directory.

Note: For explanation purposes, the code above is only pseudocode and not the same as the main.cpp file in the directory. The file has more code that allows full compilation and execution.
gcc –I <path_to_library>/include –o main.o main.c

To link against the library, the linker needs the library. The path to the library is specified using the -L switch. Also, ask the linker to link against the library using the -l switch.

gcc –I <path_to_library>/lib –o main.elf main.o -lmatrix

For detailed information on using the GCC compiler and linker switches refer to the GCC documentation.

Use a library at runtime

At runtime, the loader will look for the shared library when loading the executable. After booting the board into a Linux prompt and before executing the ELF file, add the path to the library to the LD_LIBRARY_PATH environment variable. The sd_card created when building the library already has the library, so the path to the mount point for the sd_card must be specified.

For example, if the sd_card is mounted at /mnt, use this command:

export LD_LIBRARY_PATH=/mnt