How to install/build any C++ library/package on VScode for Windows10?

  • C/C++
  • Thread starter member 657093
  • Start date
  • Tags
    C++
In summary, the conversation discusses the installation and use of the matplotlib-cpp library for plotting in C++. The library wraps the popular python library matplotlib, so a working python installation is required. The C++ code is compatible with both python2 and python3, and can be compiled using CMake or vcpkg. C++11 is currently required for building the library. The motivation for creating the library was to simplify the process of plotting data from a C++ algorithm without constantly having to update and parse files in python.
  • #1
member 657093
TL;DR Summary
Installing library for c++ using VScode on Windows 10
I'm new to c++ and I have written a code. I need to plot using matplotlibcpp at matplotlib-cpp

However I do not know how to install it. I've tried vcpkg, but it doesn't work.

I want to be able to use it in my code when I do:

#include "matplotlibcpp.h"
For example in Python, I would simply do the following to install a third party package or library:

  1. On admin cmd prompt: pip install [ package name ]
  2. On Python editor: import [ package name ]
Is there anything similar for c++ in VScode on windows?

I don't know how to use Cmake to install any library with my platform setup: Windows 10 with VScode and c++ from msys2
 
Technology news on Phys.org
  • #3
jedishrfu said:
Have you searched on C++ on vscode? I found this support page that may answer your questions.

https://code.visualstudio.com/docs/languages/cpp

There are links to more pages in this reference too.

There are numerous youtube videos on it too:

https://www.google.com/search?q=c+++on+vscode&source=lnms&tbm=vid&sa=X&ved=2ahUKEwja7fCpwYD5AhXiEVkFHYo8DbgQ_AUoAXoECAEQAw&biw=1043&bih=676
I have already set up vscode for c++ ...This is not what I'm asking...I'm asking how to install or build 3rd party library?
 
  • #4
there must be tutorial on it on youtube or the microsoft site as this would be a common task to perform.
 
  • #5
I tried searching on google...but to no avail!
 
  • #6
The matplotlib-cpp project on Github has everything you need to build except you must install the relevant tools like g++ and cmake. The readme describes what was done. You must walk thru the steps again and tell us what step you are stuck on.

I noticed its very specific about what versions of software to install so you must verify that some other version isn't sneaking in there. As an exaple, it says Python 2.7 so you must check your installed python to see what is installed ie run the python command to see if you get v2.7 or v3.x

[@home ~]$ python
Python 3.10.5 (main, Jun 9 2022, 00:00:00) [GCC 12.1.1 20220507 (Red Hat 12.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Here's the project readme instructions:

Installation

matplotlib-cpp works by wrapping the popular python plotting library matplotlib. (matplotlib.org) This means you have to have a working python installation, including development headers. On Ubuntu:

sudo apt-get install python-matplotlib python-numpy python2.7-dev

If, for some reason, you're unable to get a working installation of numpy on your system, you can define the macro WITHOUT_NUMPY before including the header file to erase this dependency.

The C++-part of the library consists of the single header file matplotlibcpp.h which can be placed anywhere.

Since a python interpreter is opened internally, it is necessary to link against libpython in order to user matplotlib-cpp. Most versions should work, although python likes to randomly break compatibility from time to time so some caution is advised when using the bleeding edge.
CMake

The C++ code is compatible to both python2 and python3. However, the CMakeLists.txt file is currently set up to use python3 by default, so if python2 is required this has to be changed manually. (a PR that adds a cmake option for this would be highly welcomed)

NOTE: By design (of python), only a single python interpreter can be created per process. When using this library, no other library that is spawning a python interpreter internally can be used.

To compile the code without using cmake, the compiler invocation should look like this:

g++ example.cpp -I/usr/include/python2.7 -lpython2.7

This can also be used for linking against a custom build of python

g++ example.cpp -I/usr/local/include/fancy-python4 -L/usr/local/lib -lfancy-python4

Vcpkg

You can download and install matplotlib-cpp using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install matplotlib-cpp

The matplotlib-cpp port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
C++11

Currently, c++11 is required to build matplotlib-cpp. The last working commit that did not have this requirement was 717e98e752260245407c5329846f5d62605eff08.

Note that support for c++98 was dropped more or less accidentally, so if you have to work with an ancient compiler and still want to enjoy the latest additional features, I'd probably merge a PR that restores support.
Why?

I initially started this library during my diploma thesis. The usual approach of writing data from the c++ algorithm to a file and afterwards parsing and plotting it in python using matplotlib proved insufficient: Keeping the algorithm and plotting code in sync requires a lot of effort when the C++ code frequently and substantially changes. Additionally, the python yaml parser was not able to cope with files that exceed a few hundred megabytes in size.

Therefore, I was looking for a C++ plotting library that was extremely easy to use and to add into an existing codebase, preferably header-only. When I found none, I decided to write one myself, which is basically a C++ wrapper around matplotlib. As you can see from the above examples, plotting data and saving it to an image file can be done as few as two lines of code.

The general approach of providing a simple C++ API for utilizing python code was later generalized and extracted into a separate, more powerful library in another project of mine, wrappy.
 
  • #7
Looking at it again, perhaps it'd be better to get vcpkg to work and avoid trying to build the project. What error did vcpkg give you?

The build instructions seems somewhat wonky with several caveats so going the vcpkg route is preferable although some security conscious folks would prefer to build the project to avoid any malware.

Another attack plan would be to have your C++ code output text files as csv files and then write a python script to ingest and plot your data making it a two step process but that should work. You'd need to write some example code to see if matplotlib for python is installed or not.

I use anaconda which installs all the major python components and that might be the best way to get them.

Also there may be third party plotting tools that would ingest and plot your data where you'd interact with the tool to get what you want.

https://brooks.digital/articles/data-visualization-tools-dont-require-it-support/
 
  • #8
jedishrfu said:
Another attack plan would be to have your C++ code output text files as csv files and then write a python script to ingest and plot your data making it a two step process but that should work. You'd need to write some example code to see if matplotlib for python is installed or not.
I am an advanced user of Python. I have Python 3.9...I just started out with c++ because I'm told it is faster.
I have already coded in Python, VBA and Julia. I have recently started coding in c++. I have a working VScode environment to code in c++ and I have already made a code. And as you mentioned, I'm already plotting through sending my data via <fstream> to a txt file and loading via np.loadtxt and plotting via Python matplotlib library.

My code runs for 1 hour in Python; the same code runs for 5 minutes in Julia and I'm trying it out on c++
However, as I'm new, I probably coded it as I would for a Python script, so the c++ code is actually slower than the others.

I have searched for comparison performance and found this article.
 
  • #9
Thats quite a healthy list of skills and programming languages.

I would stick with Julia and forget C++. There are so many reasons to hate C++ as a developer who's used it on several projects. I felt Java was a better OO language than C++ as it limited the inheritance to a single class and not multiple classes.

Some C++ developers used the notion of mixin classes to add some feature to a class hierarchy without entangling the other parts of the class and introducing duplicate attributes that depend on how you access them (ie a common grandfather class, then two parent classes of a child class the question is to get the attribute of the grandfather which parent route do you use?)

Julia may eventually eclipse Matlab as the goto numerical solution language. My only beef with Julia is the need to recompile stuff like the plot functions at the start of a session. Evaluators get turned off by this because of that startup delay. Its just too slow.
 
  • #10
Vick said:
I'm already plotting through sending my data via <fstream> to a txt file and loading via np.loadtxt and plotting via Python matplotlib library.
This is by far the best solution. matplotlib-cpp is a workaround where it is absolutely necessary to have a c++ executable which bundles matplotlib code so it can run with no external dependencies.

You don't have that need so the right thing to do is use Python for presentation of results and then it doesn't matter whether you generate those results in Python, c++ or Julia.

Edit: another good solution could be to write a Python module in c++ speeding up whatever is the bottleneck in your calculation, however if you are already making extensive use of numpy/scipy then the gains could be marginal as these routines are implemented in c++ (or Fortran) already.
 
Last edited:
  • #11
Vick said:
My code runs for 1 hour in Python; the same code runs for 5 minutes in Julia
The reason Python can be slow is often misunderstood. The key issue is dynamic typing, with p-code processing as a secondary issue. This means that in order to calculate a * b the Python runtime first has to work out what type of data a and b contain. If a and b are floats this takes many orders of magnitude more time than just performing the addition.

However if a and b are example 100x100 np.arrays where calculating a * b requires about a million flops then numpy will perform those flops just as fast as any other code* so the dynamic typing and p-code interpretation is insignificant.

* except code targetting processor-specific vector instructions
 
  • #12
jedishrfu said:
Julia may eventually eclipse Matlab as the goto numerical solution language. My only beef with Julia is the need to recompile stuff like the plot functions at the start of a session. Evaluators get turned off by this because of that startup delay. Its just too slow.
For me, my primary concern is the computation time. If Julia takes time to load or need to recompile, it doesn't matter to me. My clock starts once the actual computation begins. My main programming focus is Python. Because I can virtually do anything in my code with it. However, because my computation is extensive and I need faster computation, I have switched a little bit to Julia for the speed. I'm also looking for alternative in c++

So far, my appraisal of the 3, with regard to speed in computation, Julia tops it, then Python and lastly c++.
 
  • #13
pbuk said:
You don't have that need so the right thing to do is use Python for presentation of results and then it doesn't matter whether you generate those results in Python, c++ or Julia.
I was actually looking for matplotlib-cpp to plot because I thought, it was fstream that was too slow for my computation run on c++ But after editing my code and writing to file after the actual computation terminated, showed that it was the latter that took time and not the writing up of output.
 
  • #14
Vick said:
So far, my appraisal of the 3, with regard to speed in computation, Julia tops it, then Python and lastly c++.
This is not surprising. Julia can be fast because it has a high level API which accesses highly optimised low level implementations. Python can be almost as fast for some operations where the bottleneck is the low level calculations (e.g. calculation of eigenvalues of large matrices) however this is not always the case. A naiive hand-written routine in C++ for calculating eigenvalues will be many times slower than either (but if you link in LINPACK or Boost it should be just as fast).
 
  • Like
Likes jedishrfu
  • #15
Vick said:
I thought, it was fstream that was too slow for my computation run on c++
fstream is limited by the write speed of the disk, typicall 100 megabytes a second to 5 gigabytes a second. That is never going to be your bottleneck :biggrin:
 
  • #16
pbuk said:
A naiive hand-written routine in C++ for calculating eigenvalues will be many times slower than either (but if you link in LINPACK or Boost it should be just as fast).
Yes...that's all about my original question: how do I install Boost?
 

FAQ: How to install/build any C++ library/package on VScode for Windows10?

1. How do I install Visual Studio Code on Windows 10?

To install Visual Studio Code on Windows 10, follow these steps:

1. Go to the Visual Studio Code website and download the Windows installer.

2. Run the installer and follow the instructions to complete the installation.

3. Once installed, open VScode and you are ready to start using it.

2. How do I install a C++ library/package on VScode for Windows10?

To install a C++ library/package on VScode for Windows10, follow these steps:

1. Open your project in VScode and click on the "Terminal" tab at the top.

2. Type in "npm install " and press enter.

3. This will automatically download and install the library/package into your project folder.

3. How do I add a library to my C++ project in VScode?

To add a library to your C++ project in VScode, follow these steps:

1. Open your project in VScode and click on the "Terminal" tab at the top.

2. Type in "npm install --save" and press enter.

3. This will add the library to your project's dependencies, which will allow you to use it in your code.

4. How do I build a C++ project in VScode for Windows10?

To build a C++ project in VScode for Windows10, follow these steps:

1. Open your project in VScode and click on the "Terminal" tab at the top.

2. Type in "npm run build" and press enter.

3. This will run the build command and compile your project, creating an executable file.

5. How do I run a C++ program in VScode for Windows10?

To run a C++ program in VScode for Windows10, follow these steps:

1. Open your project in VScode and click on the "Terminal" tab at the top.

2. Type in "npm start" and press enter.

3. This will run the start command and execute your C++ program.

Similar threads

Replies
2
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
25
Views
2K
Replies
6
Views
13K
Replies
17
Views
2K
Replies
8
Views
2K
Back
Top