adding libmodbus to CMakeLists.text file


I am working on a C++ application that will run on Ubuntu 22.04. I am using Jetbrain’s C/C++ IDE called “CLion”. My application is working fine and now I wish to incorporate libmodbus into it. However, I don’t know the necessary commands needed to add libmodbus to my CMakeLists.text file. I would really appreciate some basic assistance on how to do it, so that I can can begin developing my modbus-TCP communications.

Thus far I have performed the following:

I downloaded libmodbus-3.1.10.tar.gz inside my home directory.

I created a directory called “Dev” inside my home directory.

Then I extracted libmodbus-3.1.10.tar.gz inside my “Dev” directory thus creating my “libmodbus-3.1.10” directory tree.

Inside my “libmodbus-3.1.10” directory I created a directory called “MyBuild”.

Then I went to my “MyBuild” directory and ran the following:

sudo make
sudo make install

Next I created a new test project inside CLion. Inside the project tree under External Libraries ->Header Search Path ->include (library root)->modbus are the following 4 header files (modbus.h, modbus-rtu.h, modbus-tcp.h, modbus-version.h).

Then I added the code sample from hello.c from’s website. However, I get the following errors: undefined reference to ‘modbus_connect’, ‘modbus_read_registers’, ‘modbus_close’, and ‘modbus_free’.

I can see that it can’t find the library. So would you please tell me what are the commands I need to add to my CMakeLists.txt file, so that I can use the libmodbus library in my project? I know that there are only several commands that need to be added, but I am not sure what they are.

Thanks in advance, Bob