Creating and using shared libraries

Available from 2021.0 LTS

Create a shared library

Developing a library which can be consumed by other projects is a useful feature.

Using the PLCnext CLI or using one of our provided plugins, a project with the typeconsumablelibrary can be selected.

This creates a base project for cross-compiling and creating a shared library for PLCnext Technology devices. The directories are meant to be used as following:

  • The /src folder contains all implementations and internal header files.
  • The /include folder contains all public header files.
  • The /external folder can contains dependent shared libraries (see next section).

Note: For organisational purpose, it is useful to put the header files in a sub-directory which is named after the library. As an example, there is a library called MyLib and an implementation class with the namespace MyLib::Sub::Namespace which is located in the src/Sub/Namespace directory. The regarding header files should be put into the include/MyLib/Sub/Namespace directory.

  1. Generate a new shared library:
    $ plcncli new consumablelibrary
  2. Program that library, keeping in mind to use the designated folders as described above
  3. Set the targets to build and deploy the library, e.g.:
    $ plcncli set target -a -n axcf2152 -v 2021.0
  4.  Call thebuild command:
    $ plcncli build

The resulting files and directory structure can be found in the /bin folder.

 

Note: In this template, theplcncli generate all command is not used.
Theplcncli deploy only allows the --files option to copy the output files to the destination directories.

Use shared libraries in other projects

All project templates of the PLCnext CLI include an external folder. To use a shared library, copy the whole build output (content of the bin folder) to the external folder. Make sure that the build for your project targets are included.

After that, it is necessary to make two changes to the CMakeLists.txt: (with <library-name> being the name of the added shared library):

  1. In the section add link targets , add an additional find_package(<library-name> REQUIRED) call for each shared library you want to add.
  2. Add the found package to the target_link_libraries call by adding the <library-name> at the end of the call; e.g.: 
    target_link_libraries(MyProject PRIVATE ArpDevice ArpProgramming <library-name>)

To use the above mentioned class located in theMyLib::Sub::Namespace namespace, it is necessary to include the file in the header:

#include "MyLib/Sub/Namespace/ClassName.hpp"

To use it, it is necessary to use the namespace together with the type name:

MyLib::Sub::Namespace::ClassName myVariable;

Deploy shared libraries with a project

When deploying a PLM project as a .pcwlx library the consumed library is automatically included in the .pcwlx file.

For all other project types the library needs to be deployed manually: Add the shared objects files of depending libraries into the usr/local/lib directory on the target.

 

Related Topics

  1. PLCnext CLI

 

 


•  Web browser recommendation: Chrome/Edge 88 or newer, Firefox ESR 90 or neweror Safari • 
• Published/reviewed: 2022-09-14 • Revision 046 •