Setup
Please Note:
- These instructions are very old (as of 2024).
- Better methods are available now, such as the C/C++ extension to VSCode, which can be setup with GSL using vcpkg.
- I have updated these instructions to describe a new method of setting up numerical analysis on Windows that is simpler than the old one. The older method that used Cygwin and Code::Blocks can be found here.
- The target audience for these instructions are inexperienced users (noobs with some basic knowledge of C programming) who need to use the Microsoft Windows operating system. Setup for Linux operating systems is fairly straightforward, and should not require elaboration for experienced Linux users. See this link and this video, for instance.
- The instructions given below involve using Code::Blocks and a standalone GSL library. If you need to use another IDE like Visual Studio, there are similar instructions at this website.
Summary:
On this page, I will provide instructions to set up C programming on Microsoft Windows with the Code::Blocks and the GNU Scientific Libraries in order to perform numerical analysis. There are 3 major steps, each being a collection of tasks that need to be performed in order to get everything up and running.
Glossary of Terms:
C-Compiler: Software that translates your C-program into machine language that the computer can execute.
Dynamically Linked Libraries (DLL): Functions written by someone else that you can call in your C-program.
Header Files: Files containing definitions of C-functions. Often associated with DLLs.
Linking: Connecting your program to a DLL.
GNU Scientific Library (GSL): A set of Open Source Numerical and Scientific DLLs.
Code::Blocks (IDE): An IDE (Integrated Development Environment) that makes C-programming easier and compiling/linking automatic.
Introduction:
Before starting C programming on Windows, a C-compiler needs to be installed. A compiler is a program that converts C code into executable machine code. There are several C compilers for windows. Some of them are proprietary and may require purchase, and others are FOSS, i.e. free and open source. Proprietary compilers include Microsoft Visual Studio or Intel® oneAPI DPC++/C++ Compiler. FOSS compilers include the GNU C compiler of GCC, the Gnu Compiler Collection, or the newer CLANG compiler. If in doubt, stick to GCC.
Once a compiler is installed, dynamically linked libraries (DLLs) of numerical routines should to be installed, so that their header files and functions can be used in C-programs through the process of linking. Proprietary libraries include Numerical Recipes, and FOSS libraries include GSL, the GNU Scientific Library. For a brief introduction to the GSL library, see this website. Also, there is good background material on GSL out there, such as this book by Jose M. Garrido, as well as the very well-written GSL library documentation.
In addition to compilers and DLLs, it's a good idea to have an Integrated Development Environment, or IDE. An IDE is a suite of programs that developers need, combined into one convenient package, usually with a graphical user interface. These programs include a text editor, linker, project management, and often a compiler. The instructions below can be readily adapted to most IDEs. However, I shall provide explicit instructions for Code::Blocks, a Simple IDE for C/C++/FORTRAN that is ideal for beginners.
Brief Instructions:
Download and install Code::Blocks from the link given below:
https://www.fosshub.com/Code-Blocks.html
Be sure to download "Code Blocks Windows 64 bit (including compiler)"
Download and install my pre-compiled version of GNU-Scientific Library from the link below:
https://github.com/hariseldon99/GSL-WIN64
Go to the "Releases" section and download the latest release, which will be the .EXE file.
Ignore any warnings or danger messages from antivirus or malware detection software. They're all false positives.
Start Code::Blocks and configure it as follows.
Goto "Settings--> Compiler", Click on the "Search Directories" tab. Inside, there is another "Compiler" tab. Add the path to the GSL header files. The default path is "C:\Program Files\GSL-WIN64\gsl-2.7.1\include", although your GSL version number may be newer than 2.7.1.
Then, goto the "Linker Settings" tab next to the "Search Directories" tab, click "Add" and add the paths to the installed libgsl and libgslcblas dynamically linked libraries (defaults are "C:\Program Files\GSL-WIN64\gsl-2.7.1\lib\libgsl.dll.a" and "C:\Program Files\GSL-WIN64\gsl-2.7.1\lib\libgslcblas.dll.a").
Congratulations! Code::Blocks is now ready for C-Programming with the GNU Scientific Library!
Detailed Step-by-step Instructions:
First, download Code::Blocks from codeblocks.org and install it. You can visit the website, or click on the "Download Code::Blocks" icon above.
The installation is fairly straightforward. Install the version of Code::Blocks that comes bundled with the mingw-w64 C/C++ compiler. This will be the installer labelled
codeblocks-20.03mingw-setup.exe (Note that your version number may be newer than 20.03)
That way, you can download and install the IDE and the GCC C-compiler together
Note that they also offer versions without any included compilers. Use those only if you have a compiler already setup.
For details (including screenshots) on how to download and install Code::Blocks with compiler on Windows, see this article.
Next, install the GNU Scientific Library for windows.
Ideally, they should be compiled from source code. However, this is somewhat difficult. Therefore, I am maintaining pre-compiled windows libraries for GSL.
Click here to download the GSL installer (it is the file ending in .EXE; always download the newest version), or click on the "Download GNU Scientific Library" icon above.
Double-click on the downloaded installer and install the GSL libraries. The installation is pretty straightforward. Just click "I agree" when asked to agree with the licence. In the next screen, the default installation folder is set to 'C:\Program Files\GSL-WIN64\gsl-2.7.1'. You may change it if you wish, but best don't. Note that your gsl version number may be newer than 2.7.1. Click on "Install", and then "Close" the window when when the installation is complete.
For details, see the screenshot below, from left to right.
Note that, if your antivirus software, malware detection software, or Microsoft Defender flags it down as potentially malicious, ignore or whitelist it, as it is a false positive. For Microsoft Defender Smartscreen, just click on "More info" and then click on "Run Anyway" at the bottom of the window.
3. Start Code::Blocks. Once the IDE screen loads, you have to configure Code::Blocks to link the GSL libraries for your programs just after compilation. Follow the instructions below. Note that you only have to do this step once, not every time you start Code::Blocks.
Basically, you have to tell the compiler where the GSL header files (files containing GSL function definitions, ending in .h) are. Assuming that you unpacked the previous archive to 'C:\Program Files\GSL-WIN64', the path to the headers is:
'C:\Program Files\GSL-WIN64\gsl-2.7.1\include' (Note that your GSL version number may be newer than 2.7.1).
First, access the compiler settings by clicking on 'Settings --> Compiler' from the top menu bar, as shown in the screenshot below.
Next, click on the 'Search Directories' tab, click "Add" on the bottom of the 'Compiler' tab and navigate to the above-mentioned include folder and include it in the directories to search. Once completed, the tab should look like the screenshot below. Now, Code::Blocks knows where the GSL headers are! Note that your gsl version number may be newer than 2.7.1.
Next, you have to tell Code::Blocks to link the GSL libraries to every program that you compile. These libraries were installed in step 2 above, and are found in
'C:\Program Files\GSL-WIN64\gsl-2.7.1\lib' (Note that your GSL version number may be newer than 2.7.1).
There are two libraries that have been installed as Dynamically Linked Libraries, or .dll files., 'libgsl.dll.a' (the main GSL library) and 'libgslcblas.dll.a', which is the library of Linear Algebra routines, coded according to the BLAS standard. See the 'Miscellaneous' section below for details. For now, proceed to the bullet point below.
So, click on the 'Linker settings ' tab next to the 'Compiler settings' tab and add both the above-mentioned libraries. You must do this by clicking on the "Add" button on the bottom, navigating to the GSL library folder as mentioned above, and adding the two dll files. The final result should look like the screenshot below. Note that your gsl version number may be newer than 2.7.1. Click "OK" when you're done.
Congratulations! Now, you're all set to use Code::Blocks and the GNU Scientific Library for your computational work!
Testing:
Now we make sure that the installation and configurations are working properly. In order to do so, we shall write the following simple C programs using Code::Blocks, compile and run them. The first program is a simple "Hello World" code, and the second is a more sophisticated piece of C code that uses the GSL library.
Running a simple C program on the IDE:
Start Code::Blocks.
Create a new blank file by clicking on "File --> New --> Empty file" on the top menu bar as shown in the screenshot below.
3. In the new blank file, type in (or copy-paste) the code below (a simple "Hello World" program in C), and save it to a file named 'hello.c' (Click on "File --> Save file as" from the top menu bar).
4. Click on "Build" --> "Build and Run" on the top menu bar as shown in the screenshot below. If everything was installed and configured correctly, the build should succeed, a terminal should pop up, and the code should execute there.
Now for a C code that uses GSL:
Goto to the manual page of the GSL library (click here) and copy-paste any of the sample programs provided into a new C/C++ source file in your IDE.
As an example, you can try this code simulating the time evolution of a nonlinear Van der Pol oscillator by using the ODE integrators from GSL. The code is also given below. Copy-paste it into your IDE and build/run it as before.
If everything is configured correctly, a terminal will popup with the full output, which you'll have to plot.
You can copy-paste the output (see here for instructions on how to do this) into some plotting software to plot the data. See the appendix below for details on this.
As an alternative to copy-pasting output from the terminal, you can also modify the code below to redirect the data dump (the last 'printf()') to a file via a FILE pointer, then import the file into a plotting software.
The expected output data is plotted below. You can copy-paste the columns over to Microsoft Excel and plot them there, or, alternatively, use a plotting software like SciDAVis.
Note that, if you have a lot of data in your output, it might not fit in the buffer of the command line terminal, like this
In order to access the full data, you will have to adjust the height of the terminal in the terminal settings. Right click on the title bar of the terminal and click "Properties, then "Layout". Adjust the height of the "Screen Buffer Size" to something large, like "9999", then click "OK". When you run your code again, you should get the whole data.
Miscellaneous:
The instructions above allow for linking C/C++ code to the GSL library, as well as GSL's bundled implementation of BLAS called 'gslcblas'. BLAS (Basic Linear Algebra Subprograms) is a standard for implementing vectorized routines for basic numerical linear algebra. Numerical Linear Algebra involves the study of computer algorithms that accurately and optimally perform matrix and vector operations, such as additions, inner products, matrix-vector and matrix-matrix multiplications etc. Ideally, all BLAS compatible libraries are interchangeable, so using gslcblas should be fine. However, another (more sophisticated) BLAS implementation that can be used instead of gslcblas is OpenBLAS. See the GSL manual for details.
Another way is to install MinGW-w64 separately using MSYS-2 and configuring Code::Blocks accordingly. That way, you can use the pacman manager to directly install GSL from MinGW repositories. You can also add other compilers, like clang, or GCC for FORTRAN.
Also, other excellent free IDE's that can be used similarly as Code::Blocks are CLion, Visual Studio (see this link on setting up GSL in Visual Studio), Eclipse IDE and others. See this link for details.