Transplantation and use of Qt, MinGW, libmodbus source code mode under windows

Transplantation and use of Qt, MinGW, libmodbus source code mode under windows


1 Introduction

libmodbus official website: https://libmodbus.org/

github download: https://github.com/stephane/libmodbus

As of August 26, 2023, the latest version of libmodbus is 3.1.10, and this blog is ported based on this version.


2. Transplant

The source code file of libmodbus is as follows:

Create a folder libmodbus-3.1.10 in the Qt project directory to store libmodbus source files, and copy modbus-version.h.in to the past and rename it:

Modify the modbus-version.h file, and change the version information inside to the actual version number:

Create a file called libmodbus.pri and add:


win32:LIBS += -lws2_32

HEADERS += \
    $$PWD/modbus-private.h \
    $$PWD/modbus-rtu-private.h \
    $$PWD/modbus-rtu.h \
    $$PWD/modbus-tcp-private.h \
    $$PWD/modbus-tcp.h \
    $$PWD/modbus-version.h \
    $$PWD/modbus.h

SOURCES += \
    $$PWD/modbus-data.c \
    $$PWD/modbus-rtu.c \
    $$PWD/modbus-tcp.c \
    $$PWD/modbus.c

INCLUDEPATH += $$PWD

Modify the modbus-tcp.c file, find the two header files including winsock2.h and ws2tcpip.h, and add the macro definition _WIN32_WINNT above, as shown in the following figure:

As for why this is done, see my other blog post: The MSYS and MinGW compilation environment under windows reports an error when using the network API: undefined reference to `inet_pton' solution

Then add it to the .pro file of the Qt project include($$PWD/libmodbus-v3.1.10/libmodbus.pri)!


ends…

Guess you like

Origin blog.csdn.net/qq153471503/article/details/132508739