###### tags: FPrime # Adding library to FPrime ## Index [TOC] ## Preamble It is important to add the reference to a library into each component that used it. Indeed unit test will build only the required component and the library must be built. So always include the library into all CMakeLists.txt file of component that uses it. As we will use a FreeRTOS and don't really know how they manage dynamic library, **ALWAYS USE A STATIC LIBRARY** (.a) file. for ths tuto we will add libcsp to the SocketCspIpDriver ## Location In App/Lib directory, we setup 2 directories: 1. includes: for includes files 2. lib: for the library. So you have to put your .a file into the lib directory and your includes into the includes directory. Very simple :+1: ### example #### include For lib csp we add the inlcudes files into includes directory. csp lib uses many include file so they are regroup into the cps directory. ![](https://i.imgur.com/BQfW2qv.png) #### static library The libcsp.a static file is directly added to the lib directory ![](https://i.imgur.com/YGcs3hU.png) ## CMakeLists.txt Now that the library is in the App file system we can set the compiler to use the lib in our code. For this i go in to the SocketCspIpDriver CMakeLists.txt ```cmake= # Includes include_directories(../../Lib/includes) find_library(CSP csp ../../Lib/lib) find_library(ZMQ zmq ../../Lib/lib) target_link_libraries(App_Drv_SocketCspIpDriver ${CSP} ${ZMQ}) ``` ### includes the library usually use this own include file in its code. so the includes file must be added the the cmake includes files. To be able to direclty include the lib in your code like this ```cpp= #include "csp/csp.h" ``` you need to tell the compiler the way to find the file. the following line is doing this tasks. ```cmake= include_directories(../../Lib/includes) ``` Now you can include directly any .h file that are in the csp directory. ### library Now, you need to tell the compiler were it can find the library to link the included function For thie a use the CMake command "find_library", this function will link you staic library to a variable here "CSP" ```cmake= find_library(CSP csp ../../Lib/lib) ``` Now we reference the lib we can link it with the compiler to tell him to link the library ```cmake= target_link_libraries(App_Drv_SocketCspIpDriver ${CSP} ${ZMQ}) ``` The cmake function target_link_libraries allows to link a library to the exectuable. Just link your pevious created variable to the exectuable as shown in the previous code. #### FPrime In FPrim every component is built into a static library and linked to the exectubale at the compile time. You must know the name of created library file to link your library to it. The name always follow the location of the component and the component name: For SocketCspIpDriver component we are in "App->Drv->SocketIpDriver" so the created library is ``` App_Drv_SocketCspIpDriver ``` As you can seen in cmake code below. ##### Note The only exception is the main exectuable that is named "App_exe" but it is not advised to link a library to this construct lib. As it said in the preamble the unit test of the component would not have access to the library