-1

I have a VS project (using the 2019 community edition), it's just 'hello world' in c++ that includes another header (that is not used). No matter what I try, the compiler cannot find the header. The header file is in the same directory as the .sln and 'source.cpp' files, I've tried using angle brackets and just plain quote marks, it says the 'header.hpp' file is included in the project, but I still get the following error (when I press build):

1>Source.obj : error LNK2001: unresolved external symbol _closesocket@4
1>C:\Users\hidden-r3d\source\repos\requests\Debug\requests.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "requests.vcxproj" -- FAILED.

My code is as follows:

#include <iostream>
#include "header.hpp"


int main()
{
    std::cout << "Hello World!\n";
    return 0;
}
evejohnson
  • 11
  • 2
  • The error message has nothing to do with finding a header. It is a linker error. You missed linking to `Ws2_32.lib` which for the current code example is very strange. This answer of mine can solve this problem in Visual Studio: [https://stackoverflow.com/a/16948470/487892](https://stackoverflow.com/a/16948470/487892) – drescherjm Jan 19 '21 at 04:00
  • You need to link your app to a library called `Ws2_32` – fukanchik Jan 19 '21 at 04:02
  • `requests.exe` that's *really* the name you gave you simple hellow-world example project? Something looks... wrong. – WhozCraig Jan 19 '21 at 04:03
  • @WhozCraig I was originally going to test using a requests c++ module I found, but I'm using the same project to diagnose this problem now. – evejohnson Jan 19 '21 at 04:09
  • 2
    Somewhere in your source base is a Source.cpp that is using `closesocket`, which is not resolved at linked time (because the requisite lib is missing). That has absolutely *nothing* to do with the code you partially posted, and piggybacking on a project in lord-only-knows-what state isn't going to help. Start with a clean project with *one* file. (this source file without the `header.hpp` include). Then add the header to the project, then include the header in this source file. When *that* breaks you probably have a valid [mcve] you can post in-full, and we can look at. – WhozCraig Jan 19 '21 at 04:18

1 Answers1

1

The problem is you are not linking against the Ws2_32.lib library. To fix this you can add that to your additional dependencies tab of linker/Input settings for your project. Alternatively (as pointed out by SChepurin in the comments) you can add

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

to a source file of your project.

(From https://stackoverflow.com/a/16948470/487892 as mentioned by @drescherjm in the comments of the question.)

evejohnson
  • 11
  • 2