Using vcpkg in C/C++

introduce

Nowadays, modern languages ​​such as Go often provide integrated package management to extract all dependencies of a library. However, many software are created and maintained in C/C++ and there is no ready-made package manager.

Porting software to another target platform (macOS, Windows, Linux) is often very difficult. Helpfully, there are third-party package managers that can do this. One of them is called vcpkg, which is an open source project provided by Microsoft. In the sequel, I'll show you some techniques to solve some of the difficulties in vcpkg.

All packages in vcpkg are downloaded, patched and compiled from source. So, when using large libraries like boost, ITK or OpenCV, it will take some time!

Getting started with vcpkg

Install vcpkg

Installing vcpkg is a two-step process: first, clone the repository, and then run the bootstrap script to generate the vcpkg binary. This repository can be cloned anywhere and will contain the booted vcpkg binary as well as any libraries installed from the command line. If possible, it is recommended to clone vcpkg as a submodule into an existing project for greater flexibility.
Step 1: Clone the vcpkg repository

git clone https://github.com/Microsoft/vcpkg.git

Before doing this, make sure you are in the directory where you want to install the tool.

Step 2: Run the bootstrap script to build vcpkg

./vcpkg/bootstrap-vcpkg.sh

Install the library for your project

vcpkg install [packages to install]

Using vcpkg with CMake

In order to use vcpkg with CMake outside of the IDE, you can use a toolchain file:

cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake

Then build:

cmake --build [build directory]

Using CMake, you need find_package() to reference the library in the Cmakelists.txt file.

Guess you like

Origin blog.csdn.net/weixin_42990464/article/details/132804871