PDA

View Full Version : Socket error



JS27
14th February 2014, 16:01
Hello guys,
I would like to use the Mavlink protocol inside of my QT GUI.
I defined #include "winsock2.h" to use the SOCKET structure. But I am getting error that socketconnectio is already defined.



int bytes_sent;
#define DEFAULT_PORT 14550
char target_ip[100];

SOCKET ConnectSocket;
int iResult;
typedef int socklen_t;


void socketconnection()
{
strcpy(target_ip, "127.0.0.1");

//----------------------
// Declare and initialize variables.
int iResult;
WSADATA wsaData;

ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
cout<<"WSAStartup failed with error: "<< iResult<<endl;
}
….
etc


error LNK2005: "int bytes_sent" (?bytes_sent@@3HA) is already defined in main.obj definiert.
error LNK2005: "unsigned int ConnectSocket" (?ConnectSocket@@3IA) is already defined in main.obj definiert.
error LNK2005: "int iResult" (?iResult@@3HA) is already defined in main.obj definiert.
error LNK2005: "char * target_ip" (?target_ip@@3PADA) is already defined in main.obj definiert.
error LNK2005: "void __cdecl socketconnection(void)" (?socketconnection@@YAXXZ) is already defined in main.obj definiert.
error LNK2005: "void __cdecl closesocket(void)" (?closesocket@@YAXXZ) is already defined in main.obj definiert.
error LNK2005: "void __cdecl mavlinksend(void)" (?mavlinksend@@YAXXZ)is already defined in main.obj definiert.

anda_skoa
14th February 2014, 16:39
Any reason for not using QTcpSocket or QUdpSocket (whatever applies here)?

Cheers,
_

ChrisW67
14th February 2014, 20:16
The error messages tell you the problem. The four global variables you are defining in this file are also defined in main.cpp (or whatever file compiled to main.obj). Taht os, you are asking the compiler to allocate space fro these variables in two distinct places... Probably a header file and cpp file.

This has nothing to do with Qt.