Visual Studio 2019 configures vcpkg C++ package management tool

Introduction

vcpkg helps you manage C and C++ libraries on Windows, Linux, and MacOS. In the development process, especially the configuration process using some third-party libraries is quite troublesome, and various details lead to inexplicable errors. , using vcpkg+Visual Studio 2019 saves the configuration of various dynamic and static libraries, etc., effectively improving work efficiency.

configuration

Let's take the Winsock2 library that needs to be included in network programming as an example, and use vcpkg to quickly configure the project environment.

Download and install vcpkg, use git to download or directly download the corresponding version from github to install

Download: git clone https://github.com/microsoft/vcpkg
Installation: Just run bootstrap-vcpkg.bat

Find the library we need by searching

vcpkg install Winsock2 Install the library we need vcpkg compiles and installs the x86 version of the library by default in Windows. To compile and install the x64 version, execute:

After the vcpkg install Winsock2:x64-windows command has installed the libraries we need, use vcpkg list to view our current installation and the related libraries we installed as shown in the figure below.

Using vcpkg in Visual Studio

Enter the command vcpkg integrate install (if it is not found in visual studio, you can use administrator rights)

The command vcpkg integrate project is to generate configuration for our project

Create a new vs project to view properties -> configuration properties already have the vcpkg option we installed, no need to configure any additional paths

Select Programmer Package Management Settings in Tool Options to configure vcpkg

 Add the package source to the NuGet package manager, select the source path to scripts\buildsystems under the vcpkg directory we followed, and click Update to complete the configuration

 Our vcpkg configuration is basically completed, the next step is to add it to the project, right-click the project and select Manage NuGet package, and select the custom vcpkg name we configured above for the package source to install.

 At this point, the configuration of vcpkg in Visual Studio 2019 is basically completed. Next we test whether it is successful.

#include <iostream>
#include<WinSock2.h>
#include<WS2tcpip.h>
using namespace std;
int main() {
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
	{
		return 1;
	}
	addrinfo hints{};
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	addrinfo* servinfo{};
	getaddrinfo("www.baidu.com","80",&hints,&servinfo);
	SOCKET sockfd{ socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol) };
	int connectionResult{ connect(sockfd,servinfo->ai_addr,servinfo->ai_addrlen) };
	if (connectionResult == -1)
	{
		cout << "Connection failed!" << endl;
	}
	else
	{
		cout << "connection successful!" << endl;
	}
	
	freeaddrinfo(servinfo);
	WSACleanup();
	return 0;
}

Including the Winsock2 header file, no error is found, and the test code returns normally.

Guess you like

Origin blog.csdn.net/qq_37431937/article/details/125644503