0

Full disclosure: I'm not a C/C++ programmer, I can write basic programs like a calculator or some other very basic stuff where you don't need to use external libraries.

My goal is to use this modbus C library in a proof of concept piece of code that runs just the example line (with the IP and port changed) however I cannot wrap around my head on how to use the library.

In Python I know I could just run

import library_x

and I'd use Python, however the code needs to be in C and am completely lost when it comes to add this library. My understanding is that I should compile it in a static or dynamic library (dll) but I don't know if it's the right way to go.

Basically:

I want to run the example code in the documentation in the main of a C program, how can I do it?C

I am using Windows 10 and Visual Studio 2017.

I tried to add all the header files in the library (.h) in the Visual Studio project and compile it but it did not work.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mickkk
  • 1,172
  • 2
  • 17
  • 38

1 Answers1

1

This question has been already answered here:

libmodbus in MFC

I don't use Visual Studio but even if you don't want to use the project templates it should be quite straight forward to compile the sample code whatever your tools and environment: you have to point your compiler to include the headers and link using libmodbus. On Linux, after installing the library of compiling from sources you just have to:

$ gcc -o your_modbus_example -I/usr/include/modbus your_modbus_example.c -lmodbus

The unit_test_client.c and unit_test_server.c files provided with libmodbus should compile and work out of the box.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16