Unresolved external

hi guys i had no idea of what is going on here ...
i have file a
ConnectionHeader.h:

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
47
48
#ifndef _X_H_GUARD
#define _X_H_GUARD

#ifdef UNICODE
#undef UNICODE
#endif
#ifdef _UNICODE
#undef _UNICODE
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512

#include "stdafx.h"
#include <Windows.h>
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment (lib,"Ws2_32.lib")



extern SOCKET master_socket;
extern int	collection_sock[30];
extern int	errn;
extern int	recvResult;
extern int	sendResult;
extern char sendBuffer;
extern int	CLIENT;
extern char recvBuffer[DEFAULT_BUFLEN];
extern WSADATA wsadata;
extern sockaddr_in hints,clientAddress;
extern addrinfo addInfo,
	*result;
extern fd_set fds;
extern DWORD threadId;
DWORD WINAPI HandleRequest();
extern HANDLE Thread;

int MakeConnection();
int Init();
int _verifySocket();
 
#endif 


That is #included into an Other.cpp file that as function1() and function2()
and The main file where i call function1() and fucnion2()
now the problem is that visual studio return me that there's 13 unresolved externals...
what's wrong in what i write?
Thanks for help...
Have you defined the 14 external symbols you declared in ConnectionHeader.h?
Yes i'vs declared all the variable...
This is the 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "ConnectionHeader.h"


 int Init()
{

	for(int i = 0; i < 30; i++)
	{
	collection_sock[i] = 0;
	}
	
	for(int i = 0; i < DEFAULT_BUFLEN; i++)
	{
	recvBuffer[i] = 0;
	}
	
ZeroMemory(&addInfo,sizeof(addInfo));
addInfo.ai_family = AF_INET;
addInfo.ai_protocol = IPPROTO_TCP;
addInfo.ai_socktype = SOCK_STREAM;
return 0;
}



 int MakeConnection()
{

BOOL Mode = TRUE;
int modeLen = sizeof(Mode);
int addrSize = sizeof(clientAddress);
    
errn = WSAStartup(MAKEWORD(2,2),&wsadata);
	if(errn == SOCKET_ERROR)
        printf("Errore in WSAstartUp \n");

	result = &addInfo;  

	errn = getaddrinfo(NULL,DEFAULT_PORT,&addInfo,&result);
	if(errn != 0)
		printf("Errore in getaddrinfo \n");
	
	master_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(master_socket == INVALID_SOCKET)
		printf("Errore in socket func %d\n",WSAGetLastError());

	
	errn = setsockopt(master_socket,SOL_SOCKET,SO_REUSEADDR,(char *)&Mode,modeLen);
	if(errn == SOCKET_ERROR)
		printf("Errore in setsockop %d\n",WSAGetLastError());


	errn = bind(master_socket,result->ai_addr,result->ai_addrlen);
	if(errn == SOCKET_ERROR)
		printf("Errore in bind %d\n",WSAGetLastError());

	errn = listen(master_socket,SOMAXCONN);
	if(errn == SOCKET_ERROR)
		printf("Errore in listen %d\n",WSAGetLastError());
	return 0;
}

 int _verifySocket()
{

	while(true)
	{
	FD_ZERO(&fds);
	FD_SET(master_socket,&fds);
	
	  for(int i = 1; i < 30; i++)
	  {
	    if(collection_sock[i] > 0)
			FD_SET(collection_sock[i],&fds);
	  }

	  errn = select(master_socket +1,&fds,NULL,NULL,NULL);
	  if(errn == -1)
		  printf("Errore in select \n");
	  
			  if(FD_ISSET(master_socket,&fds))
			  {
					  CLIENT = accept(master_socket,NULL,NULL);
					  if(CLIENT == -1)
						  printf("Errore in accept %d\n",WSAGetLastError());
					  FD_SET(CLIENT,&fds);
		   
					  printf("\nNew connection from socket %d ip: %s\n",CLIENT,inet_ntoa(clientAddress.sin_addr));
						Thread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)HandleRequest,(LPVOID)CLIENT,0,&threadId);
			  }

			for(int i = 1; i < 30; i++)
			{
				if(collection_sock[i] = 0)
				{
					collection_sock[i] = CLIENT;
					printf("Adding client in collection at position: %d/30\n",i);
				break;
				}
			
			}

	}
	return 0;

}
// ----------------------------------------------------------------------------------------------------


DWORD WINAPI HandleRequest(){
	
do{
	recvResult = recv(CLIENT,recvBuffer,sizeof(recvBuffer),NULL);
	switch(recvResult)
	{
	case -1: printf("\nErrore in recv %d\n",WSAGetLastError()); return -1;
		break;
	default : printf("Receiving %d byte",recvResult); return 0; ExitThread(0);
	}
}while(recvResult > 0);


}
I'm not able to solve this... some help?
What are some of the unresolved symbols? Would you mind copy and pasting a few or all of them? Are you sure that "ws2_32.lib" is the only library you need to link against?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1>Connection.obj : error LNK2001: unresolved external symbol "struct addrinfo addInfo" (?addInfo@@3Uaddrinfo@@A)
1>Connection.obj : error LNK2001: unresolved external symbol "char * recvBuffer" (?recvBuffer@@3PADA)
1>Connection.obj : error LNK2001: unresolved external symbol "int * collection_sock" (?collection_sock@@3PAHA)
1>Connection.obj : error LNK2001: unresolved external symbol "unsigned int master_socket" (?master_socket@@3IA)
1>Connection.obj : error LNK2001: unresolved external symbol "struct addrinfo * result" (?result@@3PAUaddrinfo@@A)
1>Connection.obj : error LNK2001: unresolved external symbol "int errn" (?errn@@3HA)
1>Connection.obj : error LNK2001: unresolved external symbol "struct WSAData wsadata" (?wsadata@@3UWSAData@@A)
1>Connection.obj : error LNK2001: unresolved external symbol "void * Thread" (?Thread@@3PAXA)
1>Connection.obj : error LNK2001: unresolved external symbol "unsigned long threadId" (?threadId@@3KA)
1>Connection.obj : error LNK2001: unresolved external symbol "struct sockaddr_in clientAddress" (?clientAddress@@3Usockaddr_in@@A)
1>Connection.obj : error LNK2001: unresolved external symbol "int CLIENT" (?CLIENT@@3HA)
1>Connection.obj : error LNK2001: unresolved external symbol "struct fd_set fds" (?fds@@3Ufd_set@@A)
1>Connection.obj : error LNK2001: unresolved external symbol "int recvResult" (?recvResult@@3HA)
1>C:\Users\utente\documents\visual studio 2010\Projects\Main\Debug\Main.exe : fatal error LNK1120: 13 unresolved externals


These are the error....and ws2_32.lib it's the only library i need,because it work when it is in only one file .cpp and one header...
Thanks for help :)
Why are all of those variables declared as 'extern'? Getting rid of that should fix this issue.
You have declared all those variables in your header but you have not defined them (there's a difference). You declared them as "extern" which means they should be defined externally, so you must define them externally, which you have not done. If you get rid of the "extern"s in your header it should fix your problem, as they will be declared and defined rather than just declared.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1>Main.obj : error LNK2005: "int errn" (?errn@@3HA) already defined in Connection.obj
1>Main.obj : error LNK2005: "void * Thread" (?Thread@@3PAXA) already defined in Connection.obj
1>Main.obj : error LNK2005: "struct addrinfo addInfo" (?addInfo@@3Uaddrinfo@@A) already defined in Connection.obj
1>Main.obj : error LNK2005: "char * recvBuffer" (?recvBuffer@@3PADA) already defined in Connection.obj
1>Main.obj : error LNK2005: "char sendBuffer" (?sendBuffer@@3DA) already defined in Connection.obj
1>Main.obj : error LNK2005: "struct fd_set fds" (?fds@@3Ufd_set@@A) already defined in Connection.obj
1>Main.obj : error LNK2005: "unsigned long threadId" (?threadId@@3KA) already defined in Connection.obj
1>Main.obj : error LNK2005: "int * collection_sock" (?collection_sock@@3PAHA) already defined in Connection.obj
1>Main.obj : error LNK2005: "struct sockaddr_in hints" (?hints@@3Usockaddr_in@@A) already defined in Connection.obj
1>Main.obj : error LNK2005: "int sendResult" (?sendResult@@3HA) already defined in Connection.obj
1>Main.obj : error LNK2005: "int recvResult" (?recvResult@@3HA) already defined in Connection.obj
1>Main.obj : error LNK2005: "struct WSAData wsadata" (?wsadata@@3UWSAData@@A) already defined in Connection.obj
1>Main.obj : error LNK2005: "int CLIENT" (?CLIENT@@3HA) already defined in Connection.obj
1>Main.obj : error LNK2005: "unsigned int master_socket" (?master_socket@@3IA) already defined in Connection.obj
1>Main.obj : error LNK2005: "struct sockaddr_in clientAddress" (?clientAddress@@3Usockaddr_in@@A) already defined in Connection.obj
1>Main.obj : error LNK2005: "struct addrinfo * result" (?result@@3PAUaddrinfo@@A) already defined in Connection.obj
1>C:\Users\utente\documents\visual studio 2010\Projects\Main\Debug\Main.exe : fatal error LNK1169: one or more multiply defined symbols found
1>

This happen if i delete "extern" in all of the variables...
That's one of the many reasons to avoid using global variables.
What i can do to solve this? It's quite urgent...
Stop using global variables. You have a file in your project that is called Connection.cpp which includes your header file ConnectionHeader.h that is re declaring variables with the same name as the ones in your global scope. Fix that.
Topic archived. No new replies allowed.