https://solarianprogrammer.com/2020/06/25/getting-started-mathgl-windows-linux/ Solarian Programmer My programming ramblings YouTube Patreon Twitter RSS Home Archives Contact Privacy Getting started with C++ MathGL on Windows and Linux Posted on June 25, 2020 by Paul In this article, I will show you how to install and get started with the C++ interface of MathGL on Linux and Windows. MathGL is a high quality scientific graphics library. In the first two parts of the article I will describe how to install the library and how to build a C++ MathGL example. In the third part of the article we'll use MathGL to create a 2D function graphic and save it as an image file. As a second example, we'll use the FLTK GUI library to show the same 2D function: MathGL Demo two dimensional wave curve function This article is split in three parts: * Install MathGL on Windows * Install MathGL on Linux * C++ MathGL usage example I recommend that you read the install part for your OS and the part that shows how to use MathGL to create some graphics. Install MathGL on Windows: On Windows, the recommended approach to install MathGL is to use the official MathGL binaries. From the above page, download the GPL or LGPL binaries for MinGW 64 bits. After you've finished downloading the archive, you can extract it with 7zip. Next, we are going to install the MinGW 64 GCC toolchain that was used to build the library. At the current time, easiest approach is to download Code::Blocks version 20.03. Make sure to download the file named codeblocks-20.03mingw-setup.exe. Start the Code::Blocks installer and accept the defaults. Once the installation is finished, Code::Blocks will detect the included GCC toolchain. Next, you will need to manually copy the content of the MathGL archive to the location where Code::Blocks has installed GCC, this is usually C:\Program Files\CodeBlocks\MinGW. Copy the content of the bin folder of MathGL to the the bin folder of MinGW. When asked if you want to overwrite some files with the same names, skip these files. Copy the content of the lib folder of MathGL to the the lib folder of MinGW. Open the include folder of MathGL and copy the include folder mgl2. Inside the MinGW folder, go to x86_64-w64-mingw32, open the include folder and paste the mgl2 folder in here. Open Code::Blocks and create a new project, chose Console Application and C++. Chose a name for the project and a location. Once the C++ console project is open, right click on the project name (in the left panel) and select Build options. Select Linker Settings and in the Other linker options panel add the desired libraries: -lmgl in this case. If you want to be able to use the FLTK GUI library, like you will see in our second example, you will need to add -lmgl -lmgl-fltk. Press OK. In Code::Blocks, you can use the Build and Run button (the small wheel with a green triangle icon) to compile and execute a program. Next, remove the default C++ generated code from main.cpp and go to MathGL usage part of this article for an example. Install MathGL on Linux: For Linux, I will exemplify the installation part on a Debian based system. Let's start by installing a C++ toolchain. Open a Terminal and write: 1 sudo apt install build-essential Next, install MathGL with: 1 sudo apt install libmgl-dev Assuming that you've saved your MathGL program in a file named mgl_ex1.cpp, this is how you can build it: 1 g++ mgl_ex1.cpp -o mgl_ex1 -lmgl or, if you are using the FLTK GUI library: 1 g++ mgl_ex1.cpp -o mgl_ex1 -lmgl -lmgl-fltk Once the program is compiled, you can run it as usual on Linux with: 1 ./mgl_ex1 Next, go to the MathGL usage part of this article for an example program. C++ MathGL usage example: Here is a small C++ MathGL program that will draw a wave curve and save the graphic in an image file named mgl_example.png: 1 #include 2 3 int main() { 4 mglGraph gr; 5 gr.Title("MathGL Demo"); 6 gr.SetOrigin(0, 0); 7 gr.SetRanges(0, 10, -2.5, 2.5); 8 gr.FPlot("sin(1.7*2*pi*x) + sin(1.9*2*pi*x)", "r-2"); 9 gr.Axis(); 10 gr.Grid(); 11 gr.WriteFrame("mgl_example.png"); 12 } The image will be saved where your program executable is located. Next, we'll draw the same curve, but this time using a FLTK window that will let you play with the graph: zoom, pan, rotate and other operations: 1 #include 2 3 int graph(mglGraph *gr) { 4 gr->Title("MathGL Demo"); 5 gr->SetOrigin(0, 0); 6 gr->SetRanges(0, 10, -2.5, 2.5); 7 gr->FPlot("sin(1.7*2*pi*x) + sin(1.9*2*pi*x)", "r-4"); 8 gr->Axis(); 9 gr->Grid(); 10 return 0; 11 } 12 13 int main() { 14 mglFLTK gr(graph, "MathGL demo window title"); 15 return gr.Run(); 16 } If you want to learn more about the capabilities of MathGL I recommend that you check their examples and the user manual. Show Comments --------------------------------------------------------------------- Categories C# Charts C++11 Regex Scheme Multithreading Posix Books C++ C++14 C++17 OSX Python Objective-C Windows Clang Fortran CUDA Roguelike Perlin Cling C++20 Linux WSL Fractals OpenGL JavaScript OpenCV BeagleBone Productivity Raspberry Pi OpenMP iOS Node.js macOS NumPy SciPy Matplotlib GCC Swift C C99 C11 Arduino Videos Armadillo Chromebook ChromeOS Docker x86-64 F# dotnet C17 Lisp Apple Silicon arm64 Recent posts * Install Python with NumPy SciPy Matplotlib on macOS Big Sur (Apple Silicon arm64 version) * Getting Started with Clang and Visual Studio Code on Windows with MSYS2 and MinGW-w64 * Enable OpenSSH server on Windows 10 * Using the Visual Studio Developer Command Prompt from the Windows Terminal * Getting started with C++ MathGL on Windows and Linux * Getting started with GSL - GNU Scientific Library on Windows, macOS and Linux * Install Code::Blocks and GCC 9 on Windows - Build C, C++ and Fortran programs * C++ Implementing a Chaos Game simulator * Install GCC 9 on Windows - Build C, C++ and Fortran programs * C++20 span tutorial Disclaimer: All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding. Copyright (c) 2023 - Paul Silisteanu