首先将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 -= 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
修改一些必要的头文件
工程中有些头文件,没有对在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")
运行结果