0

The compilation issue is caused by the global variable [ All_Sockets ], here is the minimal example

main.cpp

#include <winsock2.h>
#include <windows.h>
#include <vector>
#include "global.h"


int main() {

    std::vector<SOCKET> All_Sockets(10, INVALID_SOCKET);    // definition of global var

    getchar();
    return 1;
}

global.h

#pragma once
#include <vector>
#include <Windows.h>

extern std::vector<SOCKET> All_Sockets;      // declaration of global variable

file1.cpp

#include "global.h"
#include <windows.h>

void ftn1(){
    int i = 2;
    SOCKET Client_socket = All_Sockets[i];      // usage of global var
}

I'm building this project in visual studio and the following linking error occurs

1>file1.obj : error LNK2001: unresolved external symbol "class std::vector<unsigned int,class std::allocator<unsigned int> > All_Sockets"
0

1 Answer 1

2

You must define All_Sockets as global variable. Currently, All_Sockets is defined as local variable, so All_Sockets must be defined above main function.

std::vector<SOCKET> All_Sockets(10, INVALID_SOCKET);    // definition of global var

int main() {
    getchar();
    return 1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I thought only the declaration should be global. thanks for the reply :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.