天天看點

Ethercat系列(10)用QT實作SOEM主站

  1. 首先将SOEM編譯成靜态Lib庫

可以參考前面的博文

(83條消息) VS2017下編譯SOEM(Simle Open EtherCAT Master)_soem vs_CoderIsArt的部落格-CSDN部落格

make_libsoem_lib.bat "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build" x86

  1. 用QT建立控制台程式并導入靜态庫
Ethercat系列(10)用QT實作SOEM主站

工程檔案如下,

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

DEFINES += HAVE_STRUCT_TIMESPEC  #to void timespec redefinition compain

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32: LIBS += -L$$PWD/libs/ -llibsoem

INCLUDEPATH += $$PWD/libs

INCLUDEPATH += $$PWD/include

DEPENDPATH += $$PWD/libs

win32:!win32-g++: PRE_TARGETDEPS += $$PWD/libs/libsoem.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/libs/liblibsoem.a

HEADERS += \
    include/ethercatbase.h \
    include/ethercatcoe.h \
    include/ethercatconfig.h \
    include/ethercatdc.h \
    include/ethercatfoe.h \
    include/ethercatmain.h \
    include/ethercatprint.h \
    include/ethercatsoe.h \
    include/ethercattype.h \
    include/nicdrv.h \
    include/osal.h \
    include/osal_defs.h \
    include/osal_win32.h

win32: LIBS += -L$$PWD/wpcap/Lib/ -lwpcap

INCLUDEPATH += $$PWD/wpcap/Include
DEPENDPATH += $$PWD/wpcap/Include

win32:!win32-g++: PRE_TARGETDEPS += $$PWD/wpcap/Lib/wpcap.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/wpcap/Lib/libwpcap.a

unix|win32: LIBS += -L$$PWD/wpcap/Lib/ -lPacket

INCLUDEPATH += $$PWD/wpcap/Include/pcap
DEPENDPATH += $$PWD/wpcap/Include/pcap
           
  1. 修改一些必要的頭檔案

工程中有些頭檔案,沒有對在C++環境中編譯作處理,需要作如下處理。

#ifdef _cplusplus

extern "C" {

#endif

#ifdef _cplusplus

}

#endif

  1. 加入缺少的系統庫

#pragma comment(lib,"libsoem.lib")

#pragma comment(lib,"Packet.lib")

#pragma comment(lib,"wpcap.lib")

#pragma comment(lib,"ws2_32.lib")

#pragma comment(lib,"winmm.lib")

  1. 運作結果
Ethercat系列(10)用QT實作SOEM主站

繼續閱讀