0

I'm newbie in C++ programming. I started from Qt 5.0.1. I need to use WebSockets from: https://code.google.com/p/tufao/ but I don't have MinGW installation so I googled that I can create Qt library project, include these source files and build static library. So I did it:

QT       += network
QT       -= gui
TARGET = tufao
TEMPLATE = lib
CONFIG += staticlib
INCLUDEPATH = src src/priv
SOURCES += \
    src/websocket.cpp \
    src/url.cpp \
    src/simplesessionstore.cpp \
    src/sessionstore.cpp \
    src/querystring.cpp \
    src/httpserverresponse.cpp \
    src/httpserverrequestrouter.cpp \
    src/httpserverrequest.cpp \
    src/httpserver.cpp \
    src/httppluginserver.cpp \
    src/httpfileserver.cpp \
    src/headers.cpp \
    src/abstractmessagesocket.cpp \
    src/abstracthttpserverrequesthandler.cpp \
    src/priv/tcpserverwrapper.cpp \
    src/priv/rfc1123.cpp \
    src/priv/rfc1036.cpp \
    src/priv/reasonphrase.cpp \
    src/priv/http_parser.c \
    src/priv/asctime.cpp

HEADERS += \
    src/websocket.h \
    src/url.h \
    src/tufao_global.h \
    src/simplesessionstore.h \
    src/sessionstore.h \
    src/sessionsettings.h \
    src/session.h \
    src/querystring.h \
    src/ibytearray.h \
    src/httpsserver.h \
    src/httpserverresponse.h \
    src/httpserverrequestrouter.h \
    src/httpserverrequest.h \
    src/httppluginserver.h \
    src/httpfileserver.h \
    src/headers.h \
    src/abstractmessagesocket.h \
    src/abstracthttpserverrequesthandlerfactory.h \
    src/abstracthttpserverrequesthandler.h \
    src/priv/websocket.h \
    src/priv/url.h \
    src/priv/tcpserverwrapper.h \
    src/priv/simplesessionstore.h \
    src/priv/sessionstore.h \
    src/priv/rfc1123.h \
    src/priv/rfc1036.h \
    src/priv/reasonphrase.h \
    src/priv/httpsserver.h \
    src/priv/httpserverresponse.h \
    src/priv/httpserverrequestrouter.h \
    src/priv/httpserverrequest.h \
    src/priv/httpserver.h \
    src/priv/httppluginserver.h \
    src/priv/httpfileserver.h \
    src/priv/http_parser.h \
    src/priv/cryptography.h \
    src/priv/asctime.h
unix:!symbian {
    maemo5 {
        target.path = /opt/usr/lib
    } else {
        target.path = /usr/lib
    }
    INSTALLS += target
}

After build, I get libtufao.a. So next step is to test it. I created simple test project and added this library by clicking Add library -> external -> static. Open dialog can open only .lib files so I changed libtufao.a to libtufao.lib:

QT       += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets   
TARGET = sockettest
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp
HEADERS  += mainwindow.h \
FORMS    += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../components/tufao-build-Desktop_Qt_5_0_1_MinGW_32bit-Release/release/ -llibtufao
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../components/tufao-build-Desktop_Qt_5_0_1_MinGW_32bit-Release/debug/ -llibtufao

INCLUDEPATH += $$PWD/../../components/tufao/src
DEPENDPATH += $$PWD/../../components/tufao/src

win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../components/tufao-build-Desktop_Qt_5_0_1_MinGW_32bit-Release/release/libtufao.lib
else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../components/tufao-build-Desktop_Qt_5_0_1_MinGW_32bit-Release/debug/libtufao.lib

But when I try to build this test project I get compiler error:

undefined reference to `imp__ZN5Tufao9WebSocket13connectToHostERK12QHostAddresstRK10QByteArrayRKNS_7HeadersE' collect2.exe:-1:

error: ld returned 1 exit status

Can anyone help me?

Regards

EDIT: Problem solved by adding all sources into my dest project and in .pro file I added DEFINES += TUFAO_LIBRARY. Now everything is compiling fine.

NG_
  • 6,895
  • 7
  • 45
  • 67
Dibo
  • 1,159
  • 17
  • 34

1 Answers1

0

Adding external library into Qt Creator project

The linker can't find the implementation of WebSocket::connectToHost, so you need to link the library that has that function in it.

In your .pro file, add

LIBS += "path/to/lib/libname"

Or if it is in your library paths already, you may be able to put

LIBS += -lmylibname

So I think for you, might be

LIBS += -llibtufao

Hope that helps.

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Still same error. Maybe problem is in my library project which compile this lib (first code in my post)? – Dibo Feb 22 '13 at 18:50
  • ... and even if I add all these files into my test project I get houndred similar errors – Dibo Feb 22 '13 at 18:56
  • the .a file is what the file type for a static library looks like on unix. You can't just rename it to a .lib file (the static library for windows). That is probably where you are having a problem. What OS are you doing this on? – phyatt Feb 22 '13 at 19:04
  • Hmm, I compiled this lib on Windows XP 32 bit. Project created by QT Creator – Dibo Feb 22 '13 at 19:27
  • What compiler are you using? – phyatt Feb 22 '13 at 19:29
  • Shipped with QT installator (so MinGW? I'm newbie in C++). This is clean Windows XP virtual mashine, only QT Creator installed. I created new "library project" and added all websocket files – Dibo Feb 22 '13 at 19:36
  • ... and I checked "Static linked library" when created this library project – Dibo Feb 22 '13 at 19:38
  • So, I don't think you need to build this library from its source... Your options when downloading Qt 5.0.1 were Mingw or MSVS2010 as the compiler. You can check this in Qt Creator > Tools > Options > Build and Run > Kits... Looking at the downloads available for tufao, they have builds for both of these compilers. – phyatt Feb 22 '13 at 19:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24982/discussion-between-phyatt-and-dibo) – phyatt Feb 22 '13 at 19:42