在Windows上的Qt Creator中编译libmodbus会将未定义的引用抛出到' _imp _....'错误

时间:2014-04-15 01:26:55

标签: c++ c qt undefined-reference modbus

我有libmodbus(http://libmodbus.org/releases/libmodbus-3.0.6.tar.gz)的源.h和.c文件。在得知Qt足够聪明以了解b / w c和c ++文件之后,我将它们放入我的Qt项目中。

我在main.cpp中包含了modbus.h,我得到了大约50个错误(其中大多数是未定义的引用错误)。注意我确实让libmodbus在ubuntu中使用代码块工作,并且它正在进行通信。我现在正在Windows中尝试这个,Qt和codeblocks似乎都对我生气。

这是我的.pro文件。     QT + =核心     QT + =小部件

QT       -= gui

TARGET   = testModBus
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

QMAKE_CXXFLAGS  += -w
QMAKE_CFLAGS    += -w


SOURCES += main.cpp \
modbus.c \
modbus-data.c \
modbus-rtu.c \
modbus-tcp.c

HEADERS += \
config.h \
modbus.h \
modbus-private.h \
modbus-rtu.h \
modbus-rtu-private.h \
modbus-tcp.h \
modbus-tcp-private.h \
modbus-version.h \
zModBus.h \
inttypes.h \
stdint.h

当我尝试编译时,这是我的Qt输出:

18:03:02: Starting: "E:\Qt\Tools\mingw48_32\bin\mingw32-make.exe" 
E:\Qt\5.2.1\mingw48_32\bin\qmake.exe -spec win32-g++ -o Makefile ..\testModBus\testModBus.pro
E:/Qt/Tools/mingw48_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/Users/SSK/Desktop/qtProjects/build-testModBus-Desktop_Qt_5_2_1_MinGW_32bit-Release'
g++ -Wl,-s -Wl,-subsystem,console -mthreads -o release\testModBus.exe release/main.o release/modbus.o release/modbus-data.o release/modbus-rtu.o release/modbus-tcp.o  -lglu32 -lopengl32 -lgdi32 -luser32 -LE:\Qt\5.2.1\mingw48_32\lib -lQt5Widgets -lQt5Gui -lQt5Core 


release/modbus-tcp.o:modbus-tcp.c:(.text+0x24f): undefined reference to `_imp__select@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x2a3): undefined reference to `_imp__recv@16'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x2d7): undefined reference to `_imp__recv@16'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x30d): undefined reference to `_imp__shutdown@8'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x31c): undefined reference to `_imp__closesocket@4'

Makefile.Release:85: recipe for target 'release\testModBus.exe' failed
release/modbus-tcp.o:modbus-tcp.c:(.text+0x363): undefined reference to `_imp__setsockopt@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x394): undefined reference to `_imp__ioctlsocket@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x3c6): undefined reference to `_imp__connect@12'

mingw32-make[1]: Leaving directory 'C:/Users/SSK/Desktop/qtProjects/build-testModBus-Desktop_Qt_5_2_1_MinGW_32bit-Release'
makefile:34: recipe for target 'release' failed


release/modbus-tcp.o:modbus-tcp.c:(.text+0x3e2): undefined reference to `_imp__WSAGetLastError@0'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x444): undefined reference to `_imp__select@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x47f): undefined reference to `_imp__getsockopt@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x517): undefined reference to `_imp__send@16'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x597): undefined reference to `_imp__WSAStartup@8'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x624): undefined reference to `_imp__socket@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x65e): undefined reference to `_imp__htons@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x66f): undefined reference to `_imp__inet_addr@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x6c8): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x74c): undefined reference to `_imp__getaddrinfo@16'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x780): undefined reference to `_imp__socket@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x7bb): undefined reference to `_imp__freeaddrinfo@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x7d5): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x82f): undefined reference to `gai_strerrorW'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x8bc): undefined reference to `_imp__select@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x96c): undefined reference to `_imp__socket@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x9ab): undefined reference to `_imp__setsockopt@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0x9f0): undefined reference to `_imp__htons@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xa0a): undefined reference to `_imp__inet_addr@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xa2a): undefined reference to `_imp__bind@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xa45): undefined reference to `_imp__listen@8'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xa69): undefined reference to `_imp__htonl@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xa85): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xb38): undefined reference to `_imp__getaddrinfo@16'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xb89): undefined reference to `_imp__setsockopt@20'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xb99): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xbc8): undefined reference to `_imp__socket@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xbf9): undefined reference to `_imp__freeaddrinfo@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xc23): undefined reference to `_imp__bind@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xc33): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xc63): undefined reference to `_imp__listen@8'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xc77): undefined reference to `_imp__freeaddrinfo@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xca5): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xcda): undefined reference to `gai_strerrorW'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xd61): undefined reference to `_imp__accept@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xd8b): undefined reference to `_imp__inet_ntoa@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xdc8): undefined reference to `_imp__closesocket@4'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xe19): undefined reference to `_imp__accept@12'
release/modbus-tcp.o:modbus-tcp.c:(.text+0xe57): undefined reference to `_imp__closesocket@4'

e:/qt/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/bin/ld.exe: release/modbus-tcp.o: bad reloc address 0x250 in section `.rdata'
e:/qt/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/bin/ld.exe: final link failed: Invalid operation

collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [release\testModBus.exe] Error 1
mingw32-make: *** [release] Error 2
18:03:04: The process "E:\Qt\Tools\mingw48_32\bin\mingw32-make.exe" exited with code 2.

2 个答案:

答案 0 :(得分:7)

您需要在依赖项中包含ws2_32.lib。即在* .pro文件中添加以下行:

LIBS + = -lws2_32

答案 1 :(得分:1)

原因是由于其中一个头文件中的__declspec(dllexport)而生成错误。删除它,它消失了。然后它开始抛出一个新的错误,这是我目前所处的位置。