1

I have been trying to learn network programming so I wrote a simple program which checks whether a socket has been created or not. I am running it in Vs Code latest version but it gives

C:\Users\KIIT\AppData\Local\Temp\ccu4j9bq.o:server.cpp:(.text+0x20): undefined reference to `__imp_socket' collect2.exe: error: ld returned 1 exit status

I did check the internet, it says to link ws2_32.dll file but I cannot find an option to do so.

What should i do?

#include <winsock2.h>
#include <ws2tcpip.h>
#include<iostream>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
int main()
{
    int sc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sc)
        cout << "created";
    else
        cout << "not created";
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • `#pragma comment(lib, "Ws2_32.lib")` is a Visual Studio-ism. Visual Studio Code may not be using the Visual Studio C++ compiler and not support this. You likely have to explicitly add ws2_32.lib to the linker command line to link it in. My Visual Studio Code-fu is insufficient to tell you how to do this. – user4581301 Sep 01 '20 at 19:01
  • Note: Check the compiler warnings to see if the compiler is ignoring the `#pragma`. [The compiler doesn't have to tell you that a `pragma` is being ignored](https://en.cppreference.com/w/cpp/preprocessor/impl), but many do to help prevent problems like this. – user4581301 Sep 01 '20 at 19:05
  • C:\Users\KIIT\AppData\Local\Temp\ccu4j9bq.o:server.cpp:(.text+0x20): undefined reference to `__imp_socket' collect2.exe: error: ld returned 1 exit status .... this is the error log that i have getting..it doesn't say if #pragma is being ignored – Ayush Kumar Sep 01 '20 at 19:06
  • Thanks have updated the question... Though all the tutorials on the net have solution for Visual studio but the same options are not present in vs code – Ayush Kumar Sep 01 '20 at 19:10
  • 1
    On a side note: `if (sc)` is the wrong way to check for success. `socket()` returns `INVALID_SOCKET` (-1) on failure, which `if (sc)` will treat as a `true` condition. `socket()` will never return 0. Use `if (sc != INVALID_SOCKET)` instead – Remy Lebeau Sep 01 '20 at 19:11
  • If the cpp file successfully compiled (even with warnings a file will compile, but don't ignore them. Warnings are the compiler telling you that the program probably won't work the way you expect) it won't be compiled again unless the file is changed. In order to get the `warning: ignoring '#pragma comment ' [-Wunknown-pragmas]` message you may need to perform a clean build or make a trivial edit and resave the file. – user4581301 Sep 01 '20 at 19:11
  • Visual Studio is very different from Visual Studio Code. If your study materials reference Visual Studio I recommend [downloading, installing, and using a free Community edition](https://visualstudio.microsoft.com/vs/community/) to stay in step with the lessons. – user4581301 Sep 01 '20 at 19:14
  • I've tagged Visual Studio Code to attract the eyes of folks who know how to add libraries to the linker options. – user4581301 Sep 01 '20 at 19:18
  • Reopened because the duplicate will not help the asker. The duplicate covered a an error in the ordering of command line arguments. The problem here is trying to use a VS-specific `pragma` to automatically link a library in what looks to be a GCC or clang took chain. The answer is either Use Visual Studio or configure Visual Studio Code to generate a command line that links the library. – user4581301 Sep 01 '20 at 20:23
  • that's what the problem is the code doesn't generate a line that links the library and i have tried rebuilding and resaving the file still the pragma doesn't show any errors and moreover i did try using (sc!=INVALID_SOCKET) as suggested by remmy..still the error is there – Ayush Kumar Sep 02 '20 at 04:08

0 Answers0