How To Use Package Manager In C++ Windows

C++ logo frow Wikipedia

The thing I like about coding in C and C++ is the low level stuff, and the simplicity. But this simplicity is also one of the things that makes C doesn’t seem to fit for rapid development applications. Although sure it depends on how experienced the programmer is, for someone who has been doing C/C++ for a while, it’s not gonna be much of a problem. 

But for beginner C and C++ like me, coming from higher level programming languages and tools, even small things like how to use a library for example the library that we are gonna use is cURL, are already big steps that it is already hard to handle quickly.

If you are someone like me, a C/C++ beginner and want to use this very useful Curl library, on Windows, here’s how you can use Curl or any other library.  In Linux based OS maybe it could be much more straightforward, because cURL is usually already included.

Now we can easily  use any libraries, thanks to VCPKG: a C++ package manager for Windows, and probably it can work on Linux, and macOS too. You can read more about VCPKG here the official repo https://github.com/microsoft/vcpkg or from Microsoft https://docs.microsoft.com/en-us/cpp/build/vcpkg?view=msvc-160&viewFallbackFrom=vs-2017

This is how you are gonna use Vcpkg on visual studio code. 

1. We are going to build VCPKG from the source code, so make sure you have Visual studio build tools installed, if not you can read my previous aricle here

2. Now open x86 Native Tools command prompt and then clone vspkg source code and install.

git clone https://github.com/microsoft/vcpkg.git
.\vcpkg\bootstrap-vcpkg.bat

3. After successfully building the vcpkg, you can add vcpkg.exe to PATH so vkpkg can be accessed anywhere.

4. Testing Cvpkg, for this example, we will try to use cURL library, a free and easy-to-use client-side URL transfer library.

- Open command prompt or terminal

- Using Vcpkg search to search if curl is available

- Then install curl

cd your-project
vcpkg search curl
vcpkg install curl
vcpkg list
vcpkg integrate install

5. Now we are going to write a code to test if we successfully install a library using vcpkg, this example we are going to use a C/C++ project using CMAKE rather than Visual Studio Project or Nuget, so it can be cross platform, if you don’t know how to start creating a CMake project, you can read the previous article here.

6. So if you execute this install command again :

vcpkg install curl

It will tell you to put the code in your CMakeListst.txt like this:

find_package(CURL CONFIG REQUIRED)
target_link_libraries(main PRIVATE CURL::libcurl)

So do what it tells, put the lines into your CMakeListst.txt, but change “main” with your project name.

7. Now under your-project/.vscode/settings.json, if you don’t have one, then create it manually, add this code to include our installed libraries.

"cmake.configureSettings": {
  "CMAKE_TOOLCHAIN_FILE": "your_vcpkg_path/scripts/buildsystems/vcpkg.cmake"
}

8. Ctrl + shift + p, find and execute, Cmake: delete cache and reconfigure

9. Now we can test by create a example code using library curl

Those are messy code but basically the code above will download a file or webpage from the internet and store it in our project directory.

In languages like Java, Javascript, Python or PHP, it is very easy to use package managers, I don't know why in C++ it’s like not always working, and the worst thing is it doesn't work for cross platform. So you still need to customize it if you are working in a different environment.

Java has Maven and Gradle, so Javascript with its NPM, Python PIP and PHP composer. Although C and C++ have cross platform CMAKE, it is still not very convenient to use. In the Windows visual studio, they have nuget, but again I've tried and it doesn't seem to work. Either it’s because I am inexperienced or those things are just not meant to be easy to use.

The higher level language like Javascript, you can just add “packageName”:”packageVersion” into package.json and it works after NPM install. To use it, you just need to import those packages like include in C++. In cMake however it’s not that straightforward and very confusing, unless you are very expert and have been doing C/C++ for a while, you are gonna end up wasting time on just how to include a library.

Programming in C may not be recommended to do the very quick changing and demanding nature of modern software development because C is usually used by low level codes to talk directly to the machine that is not covered by high level language. C is not suited for doing user interfaces for the end user kind of thing.

Popular posts from this blog

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously

How To Connect SSH Using PEM Certificate On Windows

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

Flutter Button With Left Align Text and Icon

How To Create Spring Boot Project Using Netbeans