Как установить vcpkg в visual studio code
- Open with Desktop
- View raw
- Copy raw contents Copy raw contents
Copy raw contents
Copy raw contents
Vcpkg helps you manage C and C++ libraries on Windows, Linux and MacOS. This tool and ecosystem are constantly evolving, and we always appreciate contributions!
If you've never used vcpkg before, or if you're trying to figure out how to use vcpkg, check out our Getting Started section for how to start using vcpkg.
For short description of available commands, once you've installed vcpkg, you can run vcpkg help , or vcpkg help [command] for command-specific help.
Table of Contents
First, follow the quick start guide for either Windows, or macOS and Linux, depending on what you're using.
For more information, see Installing and Using Packages. If a library you need is not present in the vcpkg catalog, you can open an issue on the GitHub repo where the vcpkg team and community can see it, and potentially add the port to vcpkg.
After you've gotten vcpkg installed and working, you may wish to add tab completion to your shell.
Finally, if you're interested in the future of vcpkg, check out the manifest guide! This is an experimental feature and will likely have bugs, so try it out and open all the issues!
Quick Start: Windows
- Windows 7 or newer 2015 Update 3 or greater with the English language pack
First, download and bootstrap vcpkg itself; it can be installed anywhere, but generally we recommend using vcpkg as a submodule for CMake projects, and installing it globally for Visual Studio projects. We recommend somewhere like C:\src\vcpkg or C:\dev\vcpkg , since otherwise you may run into path issues for some port build systems.
To install the libraries for your project, run:
Note: This will install x86 libraries by default. To install x64, run:
You can also search for the libraries you need with the search subcommand:
In order to use vcpkg with Visual Studio, run the following command (may require administrator elevation):
If you're using CMake with Visual Studio, continue here.
In order to use vcpkg with CMake outside of an IDE, you can use the toolchain file:
With CMake, you will still need to find_package and the like to use the libraries. Check out the CMake section for more information, including on using CMake with an IDE.
For any other tools, including Visual Studio Code, check out the integration guide.
Quick Start: Unix
Prerequisites for Linux:
Prerequisites for macOS:
First, download and bootstrap vcpkg itself; it can be installed anywhere, but generally we recommend using vcpkg as a submodule for CMake projects.
To install the libraries for your project, run:
You can also search for the libraries you need with the search subcommand:
In order to use vcpkg with CMake, you can use the toolchain file:
With CMake, you will still need to find_package and the like to use the libraries. Check out the CMake section for more information on how best to use vcpkg with CMake, and CMake Tools for VSCode.
For any other tools, check out the integration guide.
Installing Linux Developer Tools
Across the different distros of Linux, there are different packages you'll need to install:
- Debian, Ubuntu, popOS, and other Debian-based distributions:
For any other distributions, make sure you're installing g++ 6 or above. If you want to add instructions for your specific distro, please open a PR!
Installing macOS Developer Tools
On macOS, the only thing you should need to do is run the following in your terminal:
Then follow along with the prompts in the windows that comes up.
You'll then be able to bootstrap vcpkg along with the quick start guide
Using vcpkg with CMake
If you're using vcpkg with CMake, the following may help!
Visual Studio Code with CMake Tools
Adding the following to your workspace settings.json will make CMake Tools automatically use vcpkg for libraries:
Vcpkg with Visual Studio CMake Projects
Open the CMake Settings Editor, and under CMake toolchain file , add the path to the vcpkg toolchain file:
Vcpkg with CLion
Open the Toolchains settings (File > Settings on Windows and Linux, CLion > Preferences on macOS), and go to the CMake settings (Build, Execution, Deployment > CMake). Finally, in CMake options , add the following line:
Unfortunately, you'll have to add this to each profile.
Vcpkg as a Submodule
When using vcpkg as a submodule of your project, you can add the following to your CMakeLists.txt before the first project() call, instead of passing CMAKE_TOOLCHAIN_FILE to the cmake invocation.
This will still allow people to not use vcpkg, by passing the CMAKE_TOOLCHAIN_FILE directly, but it will make the configure-build step slightly easier.
vcpkg supports auto-completion of commands, package names, and options in both powershell and bash. To enable tab-completion in the shell of your choice, run:
depending on the shell you use, then restart your console.
Vcpkg is an open source project, and is thus built with your contributions. Here are some ways you can contribute:
Please refer to our Contributing Guide for more details.
The code in this repository is licensed under the MIT License. The libraries provided by ports are licensed under the terms of their original authors. Where available, vcpkg places the associated license(s) in the location installed//share//copyright .
Most ports in vcpkg build the libraries in question using the original build system preferred by the original developers of those libraries, and download source code and build tools from their official distribution locations. For use behind a firewall, the specific access needed will depend on which ports are being installed. If you must install in in an "air gapped" environment, consider installing once in a non-"air gapped" environment, populating an asset cache shared with the otherwise "air gapped" environment.
vcpkg collects usage data in order to help us improve your experience. The data collected by Microsoft is anonymous. You can opt-out of telemetry by
We are happy to announce that we have released the Embedded Tools extension for Visual Studio Code. These capabilities are the same as what we recently announced for Visual Studio 2022. Used in conjunction with the new vcpkg artifact capabilities you can quickly bootstrap an embedded development machine and get started.
In this post we will walk you through how to acquire embedded tool dependencies with vcpkg, then demonstrate edit, build, deploy, and debugging directly in VS Code with new peripheral register and RTOS object views. We will demonstrate all of this with an Azure RTOS ThreadX project.
Azure RTOS ThreadX is a widely used Real Time Operating System (RTOS) used on billions of microcontrollers (MCUs) in products around the world. We have dedicated pages for VS Code usage in the getting started guides for the Azure IoT DevKit, NXP 1060, and STMicro STM32L4_L4+ IOT01A boards (L475E or L4S5I). The techniques used for interacting with these boards should be applicable to other targets by changing configuration information we’ll highlight throughout.
Installation
To get started install VS Code, then in the extensions side bar search for Embedded Tools and install it.
Using vcpkg to acquire embedded tools
One of the challenges in embedded development is getting the tools necessary installed and configured. It is not uncommon to have different compilers used for different projects, custom debug tools, etc. This is where the new artifact capabilities in vcpkg can help us. Today we’ll demonstrate using vcpkg from a PowerShell prompt to activate our embedded development environment, but you can use the shell you prefer on your platform. In a future release this will be integrated into a Visual Studio Code extension for a more seamless experience.
From a PowerShell prompt, clone the Azure RTOS getting started repo. This sample will serve as our example project for the remainder of this post.
Navigate to the subfolder for the board you are using, here I’m using the Azure IoT DevKit.
If you haven’t already, install vcpkg.
In the project folder, there is a file vcpkg_configuration.json. This manifest file was created with vcpkg and has recorded the tools you need to build and debug this project. See the vcpkg artifacts post for details on how to create your own manifest. Running the vcpkg activate command will use this file to determine if I have those tools, install them if I do not, then activate them in my environment for use.
So, in three commands we have cloned our embedded project, installed vcpkg, acquired and activated the necessary tools for building the project.
Using VS Code for embedded development
In the previous section we used vcpkg to acquire the tools for our embedded project. Today vcpkg is not integrated in Visual Studio Code. So, at present we need to launch VS Code from the environment we activated at the command line so those tools are available for its use as well.
Now that we have the project open in VS Code it is ready to go as any other CMake project. VS Code can run the CMake build using the preconfigured CMakePresets file with the compilers vcpkg acquired. Select the preset arm-gcc-cortex-m4 when prompted on opening the folder.
All the editing power of VS Code is now available for use with the embedded project. With this Azure RTOS project we’ll cover code navigation, IntelliSense, build, deploy, debugging. We’ll also cover new diagnostic capabilities around peripheral registers and RTOS object views.
Code Navigation and IntelliSense
Use Ctrl + T to open symbol search and type button_a_callback, select the button_a_callback function to navigate to it. Note on hover that this function expands to TIM3->CCR1 which is a peripheral register on the board which we’ll inspect while we are debugging.
Build, Deploy, and Debug on hardware
Set a breakpoint on RGB_LED_SET_R().
To see how the launch is controlled in the sidebar open .vscode/launch.json and look at the launch configuration. You can see the miDebuggerPath set to use arm-none-eabi-gdb while the debugServerPath is set to invoke openocd with the configuration script in debugServerArgs. The information for describing peripheral registers is provided by the MCU manufacturer in an SVD file that we point to with svdPath. This project is configured to break on entry with the property stopAtConnect.
Now, with your board plugged into the PC with ST-LINK drivers installed, select the debug icon in the activity bar and make sure Run and Debug is set to Launch. Now F5 will work using openocd acquired by vcpkg to flash and debug the attached board. Execution stops at the entry function, so here we see the assembly code for that. We can also view the disassembly by right clicking the function in the call stack window, Open Disassembly View.
Press continue to let execution continue.
Peripheral Registers
Now press the A button on the board. This has triggered our breakpoint. We can now go to the command palette Ctrl + Shift + P and enter “peripheral”, select Focus on Peripheral View View. This opens the peripheral register view using the information from our SVD file specified in the launch configuration. We can now scroll through this list to find the register we are interested in, here TIM3->CCR1 which will update when we step over the RGB_LED_SET_R function.
RTOS Object Views
VS Code threads view won’t show you the threads in our Azure RTOS application. Why not? A good explanation can be found in this white paper on Real Time Operating Systems.
“A Real-Time Operating System is system software that provides services and manages processor resources for applications. These resources include processor cycles, memory, peripherals, and interrupts. The main purpose of a Real-Time Operating System is to allocate processing time among various duties the embedded software must perform. This typically involves a division of the software into pieces, commonly called “tasks” or “threads,” and creating a run-time environment that provides each thread with its own virtual microprocessor (“Multithreading”). Basically, a virtual microprocessor consists of a virtual set of microprocessor resources, e.g., register set, program counter, stack memory area, and a stack pointer. Only while executing does a thread use the physical microprocessor resources, but each thread retains its own copy of the contents of these resources as if they were its own private resources (the thread”s “context”).”
So for embedded development ideally you would have views that could show you information about these RTOS objects. To see the new available RTOS objects views, go to the command palate Ctrl + Shift + P and enter “rtos” then select Focus on RTOS viewer view
This will open the threads view that shows at a glance much of the information you would commonly need during embedded development, thread ids and names, how many times they have run, where the stack starts, ends, and its size as well as the maximum stack usage.
There are other views for additional ThreadX objects like block and byte pools, events, mutexes, queues, and timers. You can find these by selecting the three dots in the RTOS Viewer panel.
We also have RTOS object views for FreeRTOS though the available objects there differ based on what it supports.
Hardware breakpoint limits
One last debugging capability to call out is that embedded hardware targets commonly have a limited set of hardware breakpoints available. Setting too many can corrupt the debugger state. As such we have added hardwareBreakpoints to launch.json. For this board that is set to what it supports which is 6. If we set more than that we will see the breakpoint is not set but will show as a gray empty circle.
If you mouse over the breakpoint in the breakpoints view you will see a warning that it won’t be hit. This prevents corrupting the connection to the board and allows us the opportunity to unset some other breakpoints and continue debugging uninterrupted.
Send us your feedback
Installing vcpkg is a two-step process: first, clone the repo, then run the bootstrapping script to produce the vcpkg binary. The repo can be cloned anywhere, and will include the vcpkg binary after bootstrapping as well as any libraries that are installed from the command line. It is recommended to clone vcpkg as a submodule for CMake projects, but to install it globally for MSBuild projects. If installing globally, we recommend a short install path like: C:\src\vcpkg or C:\dev\vcpkg , since otherwise you may run into path issues for some port build systems.
Step 1: Clone the vcpkg repo
Make sure you are in the directory you want the tool installed to before doing this.
Step 2: Run the bootstrap script to build vcpkg
vcpkg install [packages to install]
vcpkg integrate install
After this, you can create a new project or open an existing one in the IDE. All installed libraries should already be discoverable by IntelliSense and usable in code without additional configuration.
cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake
Then build with:
cmake --build [build directory]
With CMake, you will need to use find_package() to reference the libraries in your Cmakelists.txt files.
Installing vcpkg is a two-step process: first, clone the repo, then run the bootstrapping script to produce the vcpkg binary. The repo can be cloned anywhere, and will include the vcpkg binary after bootstrapping as well as any libraries that are installed from the command line. It is recommended to clone vcpkg as a submodule to an existing project if possible for greater flexibility.
Step 1: Clone the vcpkg repo
Make sure you are in the directory you want the tool installed to before doing this.
Step 2: Run the bootstrap script to build vcpkg
vcpkg install [packages to install]
cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake
Then build with:
cmake --build [build directory]
With CMake, you will need to find_package() to reference the libraries in your Cmakelists.txt files.
Installing vcpkg is a two-step process: first, clone the repo, then run the bootstrapping script to produce the vcpkg binary. The repo can be cloned anywhere, and will include the vcpkg binary after bootstrapping as well as any libraries that are installed from the command line. It is recommended to clone vcpkg as a submodule to an existing project if possible for greater flexibility.
Step 1: Clone the vcpkg repo
Make sure you are in the directory you want the tool installed to before doing this.
Step 2: Run the bootstrap script to build vcpkg
vcpkg install [packages to install]
cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake
Then build with:
cmake --build [build directory]
With CMake, you will need to find_package to reference the libraries in your Cmakelists.txt files.
First, we need to know what name SQLite goes by in the ports tree. To do that, we'll run the search command and inspect the output:
Looking at the list, we can see that the port is named "sqlite3". You can also run the search command without arguments to see the full list of packages.
Installing is then as simple as using the install command.
We can check that sqlite3 was successfully installed for x86 Windows desktop by running the list command.
To install for other architectures and platforms such as Universal Windows Platform or x64 Desktop, you can suffix the package name with : .
See .\vcpkg help triplet for all supported targets.
Step 2: Use
VS/MSBuild Project (User-wide integration)
The recommended and most productive way to use vcpkg is via user-wide integration, making the system available for all projects you build. The user-wide integration will prompt for administrator access the first time it is used on a given machine, but afterwards is no longer required and the integration is configured on a per-user basis.
Note: You will need to restart Visual Studio or perform a Build to update intellisense with the changes.
You can now simply use File -> New Project in Visual Studio and the library will be automatically available. For SQLite, you can try out their C/C++ sample.
To remove the integration for your user, you can use .\vcpkg integrate remove .
CMake (Toolchain File)
The best way to use installed libraries with cmake is via the toolchain file scripts\buildsystems\vcpkg.cmake . To use this file, you simply need to add it onto your CMake command line as: -DCMAKE_TOOLCHAIN_FILE=D:\src\vcpkg\scripts\buildsystems\vcpkg.cmake .
If you are using CMake through Open Folder with Visual Studio you can define CMAKE_TOOLCHAIN_FILE by adding a "variables" section to each of your CMakeSettings.json configurations:
Note: It might be necessary to delete the CMake cache folder of each modified configuration, to force a full regeneration. In the CMake menu, under Cache () you'll find Delete Cache Folders .
Now let's make a simple CMake project with a main file.
Then, we build our project in the normal CMake way:
Note: The correct sqlite3.dll is automatically copied to the output folder when building for x86-windows. You will need to distribute this along with your application.
Handling libraries without native cmake support
Unlike other platforms, we do not automatically add the include\ directory to your compilation line by default. If you're using a library that does not provide CMake integration, you will need to explicitly search for the files and add them yourself using find_path() and find_library() .
The buildsystem-specific integration styles have heuristics to deduce the correct triplet. This can be overridden in a native way for MSBuild and CMake.
MSBuild Integration (Visual Studio)
If you are using manifest mode( vcpkg.json ) see here for additional configuration options.
User-wide integration
This will implicitly add Include Directories, Link Directories, and Link Libraries for all packages installed with vcpkg to all VS2015, VS2017 and VS2019 MSBuild projects. We also add a post-build action for executable projects that will analyze and copy any DLLs you need to the output folder, enabling a seamless F5 experience.
For the vast majority of libraries, this is all you need to do -- just File -> New Project and write code! However, some libraries perform conflicting behaviors such as redefining main() . Since you need to choose per-project which of these conflicting options you want, you will need to add those libraries to your linker inputs manually.
Here are some examples, though this is not an exhaustive list:
- Gtest provides gtest , gmock , gtest_main , and gmock_main
- SDL2 provides SDL2main
- SFML provides sfml-main
- Boost.Test provides boost_test_exec_monitor
To get a full list for all your installed packages, run vcpkg owns manual-link .
If you are using manifest mode ( vcpkg.json ) see here for all available options.
Per-project integration
Note: This approach is not recommended for new projects, since it makes them difficult to share with others.
For a portable, self-contained NuGet package, see the export command
We also provide individual VS project integration through a NuGet package. This will modify the project file, so we do not recommend this approach for open source projects.
Note: The generated NuGet package does not contain the actual libraries. It instead acts like a shortcut (or symlink) to the vcpkg install and will "automatically" update with any changes (install/remove) to the libraries. You do not need to regenerate or update the NuGet package.
Changing the triplet
You can see the automatically deduced triplet by setting your MSBuild verbosity to Normal or higher:
Shortcut: Ctrl+Q "build and run"
Tools -> Options -> Projects and Solutions -> Build and Run -> MSBuild project build output verbosity
To override the automatically chosen triplet, you can specify the MSBuild property vcpkgTriplet in your .vcxproj . We recommend adding this to the Globals PropertyGroup.
CMake Integration
Projects configured with the vcpkg toolchain file will have the appropriate vcpkg folders added to the cmake search paths. This makes all libraries available to be found through find_package() , find_path() , and find_library() .
See Installing and Using Packages Example: sqlite for a fully worked example using our CMake toolchain.
Note that we do not automatically add ourselves to your compiler include paths. To use a header-only library, simply use find_path() , which will correctly work on all platforms:
If you are using manifest mode ( vcpkg.json ) see here for all available options.
For different IDE integrations see here.
Using an environment variable instead of a command line option
The CMAKE_TOOLCHAIN_FILE setting simply must be set before the project() directive is first called. This means that you can easily read from an environment variable to avoid passing it on the configure line:
Using multiple toolchain files
To use an external toolchain file with a project using vcpkg, you can set the cmake variable VCPKG_CHAINLOAD_TOOLCHAIN_FILE on the configure line:
Alternatively, you can include the vcpkg toolchain at the end of the primary toolchain file:
Note: vcpkg does not see the settings in your own triplets, such as your compiler or compilation flags. To change vcpkg's settings, you must make a custom triplet file (which can share your own toolchain)
Changing the triplet
You can set VCPKG_TARGET_TRIPLET on the configure line:
If you use VCPKG_DEFAULT_TRIPLET environment variable to control the unqualified triplet in vcpkg command lines you can default VCPKG_TARGET_TRIPLET in CMake like Using an environment variable instead of a command line option:
Manual Compiler Setup
Libraries are installed into the installed\ subfolder in classic mode, partitioned by triplet (e.g. x86-windows):
- The header files are installed to installed\x86-windows\include
- Release .lib files are installed to installed\x86-windows\lib or installed\x86-windows\lib\manual-link
- Release .dll files are installed to installed\x86-windows\bin
- Debug .lib files are installed to installed\x86-windows\debug\lib or installed\x86-windows\debug\lib\manual-link
- Debug .dll files are installed to installed\x86-windows\debug\bin
See your build system specific documentation for how to use prebuilt binaries.
On Windows dynamic triplets: To run any produced executables you will also need to either copy the needed DLL files to the same folder as your executable or prepend the correct bin\ directory to your path.
Export Command
This command creates a shrinkwrapped archive containing a specific set of libraries (and their dependencies) that can be quickly and reliably shared with build servers or other users in your organization.
- --nuget : NuGet package
- --zip : Zip archive
- --7zip : 7Zip archive
- --raw : Raw, uncompressed folder
Each of these have the same internal layout which mimics the layout of a full vcpkg instance:
- installed\ contains the installed package files
- scripts\buildsystems\vcpkg.cmake is a toolchain file suitable for use with CMake
Additionally, NuGet packages will contain a build\native\vcpkg.targets that integrates with MSBuild projects.
Читайте также: