You are not logged in.

1

Saturday, April 24th 2010, 12:41am

Error linking against libws2_32

Hello,

I'm developing a Qt (4.6.1) application with QtCreator (1.3.1), but I'm not able to link it agains libws2_32

The .pro file:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -------------------------------------------------
# Project created by QtCreator 2010-01-21T00:17:12
# -------------------------------------------------
TARGET = RSOClient
TEMPLATE = app
#LIBS += -LE:\boost\bin.v2\libs\regex\build\gcc-mingw-4.4.0\release\link-static\threading-multi -lboost_regex-mgw44-mt-1_42
LIBS += -lws2_32 -lwsock32
SOURCES += main.cpp \
 mainwindow.cpp \
 ../../win/sockets/clsSocket/clsSocket.cpp \
 ../../clsClientSocket/clsClientSocket.cpp \
 ../../utils/string_functions.cpp
HEADERS += mainwindow.hpp \
 ../../win/sockets/clsSocket/clsSocket.hpp \
 ../../clsClientSocket/clsClientSocket.hpp \
 ../../utils/string_functions.hpp
FORMS += mainwindow.ui
INCLUDEPATH += ../../win \
 e:/boost


And the compiler's (mingw) output is:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Ejecutando los pasos para construir el proyecto RSOClient...
Iniciando: E:/Qt/2010.01/mingw/bin/mingw32-make.exe clean -w
mingw32-make: Entering directory `E:/eloy/c++/RSOQtClient/RSOClient'
e:\qt\2010.01\qt\bin\qmake.exe -spec ..\..\..\..\Qt\2010.01\qt\mkspecs\win32-g++ -win32 -o Makefile RSOClient.pro
mingw32-make: Leaving directory `E:/eloy/c++/RSOQtClient/RSOClient'
mingw32-make: Entering directory `E:/eloy/c++/RSOQtClient/RSOClient'
E:/Qt/2010.01/mingw/bin/mingw32-make -f Makefile.Debug clean
mingw32-make[1]: Entering directory `E:/eloy/c++/RSOQtClient/RSOClient'
del debug\moc_mainwindow.cpp
del ui_mainwindow.h
del debug\main.o debug\mainwindow.o debug\clsSocket.o debug\clsClientSocket.o debug\string_functions.o debug\moc_mainwindow.o
No se encuentra E:\eloy\c++\RSOQtClient\RSOClient\debug\moc_mainwindow.cpp
mingw32-make[1]: Leaving directory `E:/eloy/c++/RSOQtClient/RSOClient'
E:/Qt/2010.01/mingw/bin/mingw32-make -f Makefile.Release clean
mingw32-make[1]: Entering directory `E:/eloy/c++/RSOQtClient/RSOClient'
del release\moc_mainwindow.cpp
del ui_mainwindow.h
del release\main.o release\mainwindow.o release\clsSocket.o release\clsClientSocket.o release\string_functions.o release\moc_mainwindow.o
mingw32-make[1]: Leaving directory `E:/eloy/c++/RSOQtClient/RSOClient'
mingw32-make: Leaving directory `E:/eloy/c++/RSOQtClient/RSOClient'
No se encuentra E:\eloy\c++\RSOQtClient\RSOClient\release\moc_mainwindow.cpp
No se encuentra E:\eloy\c++\RSOQtClient\RSOClient\ui_mainwindow.h
No se encuentra E:\eloy\c++\RSOQtClient\RSOClient\release\main.o
Finalizado retornando 0.
Configuration intacta, saltando paso QMake.
Iniciando: E:/Qt/2010.01/mingw/bin/mingw32-make.exe -w
mingw32-make: Entering directory `E:/eloy/c++/RSOQtClient/RSOClient'
E:/Qt/2010.01/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `E:/eloy/c++/RSOQtClient/RSOClient'
e:\Qt\2010.01\qt\bin\uic.exe mainwindow.ui -o ui_mainwindow.h
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2010.01\qt\include\QtCore" -I"..\..\..\..\Qt\2010.01\qt\include\QtGui" -I"..\..\..\..\Qt\2010.01\qt\include" -I"..\..\win" -I"..\..\..\..\boost" -I"..\..\..\..\Qt\2010.01\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2010.01\qt\mkspecs\win32-g++" -o debug\main.o main.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2010.01\qt\include\QtCore" -I"..\..\..\..\Qt\2010.01\qt\include\QtGui" -I"..\..\..\..\Qt\2010.01\qt\include" -I"..\..\win" -I"..\..\..\..\boost" -I"..\..\..\..\Qt\2010.01\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2010.01\qt\mkspecs\win32-g++" -o debug\mainwindow.o mainwindow.cpp
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2010.01\qt\include\QtCore" -I"..\..\..\..\Qt\2010.01\qt\include\QtGui" -I"..\..\..\..\Qt\2010.01\qt\include" -I"..\..\win" -I"..\..\..\..\boost" -I"..\..\..\..\Qt\2010.01\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2010.01\qt\mkspecs\win32-g++" -o debug\clsSocket.o ..\..\win\sockets\clsSocket\clsSocket.cpp
..\..\win\sockets\clsSocket\clsSocket.cpp: In member function 'bool clsSocket::ConnectSocket()':
..\..\win\sockets\clsSocket\clsSocket.cpp:416: error: 'getaddrinfo' no se declaró en este ámbito
..\..\win\sockets\clsSocket\clsSocket.cpp:441: error: 'freeaddrinfo' no se declaró en este ámbito
..\..\win\sockets\clsSocket\clsSocket.cpp: In member function 'bool clsSocket::Listen()':
..\..\win\sockets\clsSocket\clsSocket.cpp:713: error: 'getaddrinfo' no se declaró en este ámbito
..\..\win\sockets\clsSocket\clsSocket.cpp:751: error: 'freeaddrinfo' no se declaró en este ámbito
mingw32-make[1]: Leaving directory `E:/eloy/c++/RSOQtClient/RSOClient'
mingw32-make[1]: *** [debug/clsSocket.o] Error 1
mingw32-make: Leaving directory `E:/eloy/c++/RSOQtClient/RSOClient'
mingw32-make: *** [debug] Error 2
Finalizado retornando 2.
Error durante la construcción del proyecto RSOClient
Mientras se ejecutaba la etapa 'Make'


QtCreator is in spanish, and the errors translated to english are:

..\..\win\sockets\clsSocket\clsSocket.cpp: In member function 'bool clsSocket::ConnectSocket()':
..\..\win\sockets\clsSocket\clsSocket.cpp:416: error: 'getaddrinfo' was not declared in this scope
..\..\win\sockets\clsSocket\clsSocket.cpp:441: error: 'freeaddrinfo' was not declared in this scope
..\..\win\sockets\clsSocket\clsSocket.cpp: In member function 'bool was not declared in this scope
..\..\win\sockets\clsSocket\clsSocket.cpp:713: error: 'getaddrinfo' was not declared in this scope
..\..\win\sockets\clsSocket\clsSocket.cpp:751: error: 'freeaddrinfo' was not declared in this scope

As you can see in the g++ calls the is no library at all, what makes no sense because the .pro file has the LIBS var defined. If I open the Makefile.debug file, I can see that the libraries are well defined:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
CC            = gcc
CXX           = g++
DEFINES       = -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN
CFLAGS        = -g -Wall $(DEFINES)
CXXFLAGS      = -g -frtti -fexceptions -mthreads -Wall $(DEFINES)
INCPATH       = -I"..\..\..\..\Qt\2010.01\qt\include\QtCore" -I"..\..\..\..\Qt\2010.01\qt\include\QtGui" -I"..\..\..\..\Qt\2010.01\qt\include" -I"..\..\win" -I"..\..\..\..\boost" -I"..\..\..\..\Qt\2010.01\qt\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\2010.01\qt\mkspecs\win32-g++"
LINK        =        g++
LFLAGS        =        -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows
LIBS        =        -L"e:\Qt\2010.01\qt\lib" -lmingw32 -lqtmaind -lws2_32 -lwsock32 -lQtGuid4 -lQtCored4 
QMAKE         = e:\qt\2010.01\qt\bin\qmake.exe
IDC           = e:\Qt\2010.01\qt\bin\idc.exe
IDL           = midl
ZIP           = zip -r -9
DEF_FILE      = 
RES_FILE      = 
COPY          = copy /y
COPY_FILE     = $(COPY)
COPY_DIR      = xcopy /s /q /y /i
DEL_FILE      = del
DEL_DIR       = rmdir
MOVE          = move
CHK_DIR_EXISTS= if not exist
MKDIR         = mkdir
INSTALL_FILE    = $(COPY_FILE)
INSTALL_PROGRAM = $(COPY_FILE)
INSTALL_DIR     = $(COPY_DIR)


Does anybody know what am I doing wrong?


Best regards,

Eloy.

2

Saturday, April 24th 2010, 7:27am

It is not linking step. It is compilation step.
Include appropriate header file
http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx (with examples)

Quoted


Header Ws2tcpip.h, Ws2tcpip.h on Windows 2000 (include Wspiapi.h)


Why is QTcpSocket (QtNetwork module) rejected?
Fighting fire with fire.
Three can keep a secret if two of them are dead.

3

Saturday, April 24th 2010, 10:00am

The source file that triggers the error (clsSocket.cpp) has that header included:

Source code

1
2
3
4
5
6
#include "clsSocket.hpp"
#include <sstream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <boost/regex.hpp>


The application works at a very low network level and that was the reason why I used the winsocks API. Besides I am very used to the winsocks (in raw c++ development, not in visual C++) and the BSD socket API.

Don't you think it's kinda strange that the -l libraries don't appear in the G++ call?

Regards,

Eloy.

4

Sunday, April 25th 2010, 7:33am

Source code

1
2
3
4
5
6
7
8
9
#if (_WIN32_WINNT >= 0x0501)
void WSAAPI freeaddrinfo (struct addrinfo*);
int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
		        struct addrinfo**);
int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
		       char*,DWORD,int);
#else
/* FIXME: Need WS protocol-independent API helpers.  */
#endif

It is an excerpt from ws2tcpip.h. If _WIN32_WINNT is undefined or version is set lower then the functions are not present.

Add to .cpp file before any includes.

Source code

1
2
3
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

Or define _WIN32_WINNT globaly (in .pro file see qmake documentation).

Quoted


Don't you think it's kinda strange that the -l libraries don't appear in the G++ call?

At this build stage - no.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

5

Sunday, April 25th 2010, 11:29pm

Oh I didn't remember that hack, thank you very much. I used it in one old win 98 environment because it was needed when using winsocks in old windows versions... but now I'm developing in a win XP.



Well, thank you again, it worked ;)



Regards,



Eloy.