Ethercat系列(10)用QT实现SOEM主站
首先将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
用QT创建控制台程序并导入静态库

工程文件如下,
QT -= guiCONFIG += 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_WARNINGSDEFINES += 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.0SOURCES += \main.cpp# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetwin32: LIBS += -L$$PWD/libs/ -llibsoemINCLUDEPATH += $$PWD/libsINCLUDEPATH += $$PWD/includeDEPENDPATH += $$PWD/libswin32:!win32-g++: PRE_TARGETDEPS += $$PWD/libs/libsoem.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/libs/liblibsoem.aHEADERS += \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.hwin32: LIBS += -L$$PWD/wpcap/Lib/ -lwpcapINCLUDEPATH += $$PWD/wpcap/Include
DEPENDPATH += $$PWD/wpcap/Includewin32:!win32-g++: PRE_TARGETDEPS += $$PWD/wpcap/Lib/wpcap.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/wpcap/Lib/libwpcap.aunix|win32: LIBS += -L$$PWD/wpcap/Lib/ -lPacketINCLUDEPATH += $$PWD/wpcap/Include/pcap
DEPENDPATH += $$PWD/wpcap/Include/pcap
修改一些必要的头文件
工程中有些头文件,没有对在C++环境中编译作处理,需要作如下处理。
#ifdef _cplusplus
extern "C" {
#endif
#ifdef _cplusplus
}
#endif
加入缺少的系统库
#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")
运行结果
