1

I am using VS2019 on Windows to edit c++ code files. The code is for Linux OS and depends on headers like #include <sys/socket.h> There is no project file for the visual studio so I can only open it as directory with files. It does know includes stdint.h and windows.h but not sys/socket.h.

How set VS to know the standard Linux headers? And how to include and arbitrary files for the intellisense when there is no project file (I can not create one).

I do not intent to build anything only to make intellisense and code completion possible.

Petr
  • 83
  • 7

1 Answers1

1

Linux header files in VS wihout project

Actually, <sys/socket.h> is used for UNIX/Linux. And Windows cannot use it.

Instead, you should use <Winsock2.h> on Windows and it corresponds to socket.h on Linux. Also, do not forget to link against Ws2_32.lib.

Suggestion

Use these:

#include<winsock2.h>
#pragma comment(lib, "Ws2_32.lib")

=====================

Update

I assume your project is cmake for linux.

First, you should install the related component for cmake in vs installer. See this document.

Then, add include_directories(${YOUR_DIRECTORY}) in cmakelist.txtfile to include the directory of thesocket.h` library. See cmake project to include library directories.

After that, you could include that header in cpp file.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • As I already wrote the code is for linux not for windows, I am only editing it in windows. It is not possible to build or compile it there. If it was a project like .vcxproj. I would use a pre-include to create all declarations for code checker but it is just folder with bunch of files. I want to know how to set a pre-include if it is opened as a plain directory – Petr Jul 08 '20 at 12:13
  • Hi Perry, your answer has absolutely nothing to do with my problem. I am in no way trying to use sockets on windows. I am sure you answer wold be perfect for that topic and you should move it there. Here it confuses people with the same problem as I have. – Petr Jul 09 '20 at 22:12
  • It seems that you have a cmake project for linux, right? To use cmake in vs, [you should install the related component](https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=vs-2019) in vs installer. Then you should use `include_directories(${YOUR_DIRECTORY})` in `cmakelist.txt` file to include the directory of `socket.h`. – Mr Qian Jul 10 '20 at 10:02
  • It is combination of bash scripts, make files etc. They are meaningless in windows and some are not even part of the the version controlled directory. so the IDE really sees just bunch of files where some of them are cpp or h. But maybe I could create new "cmake" file just for the VS that would be ignored by the build process in Linux. Thanks for the idea. – Petr Jul 10 '20 at 14:02
  • Thanks for your feedback and I suggest you could use cmake to combine your project folders for your situation. You can follow [this link](https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake). – Mr Qian Jul 13 '20 at 10:05