Friday, June 04, 2021

How to install GNU Scientific Library on Ubuntu

GNU Scientific Library (GSL) is an essential library for scientific computing using C. You may have to use this library to generate random numbers or solve a system of ODEs. Installing GSL on a Linux machine is straightforward. Even then some students find it difficult. Here is a step-by-step guide to install GSL on your Ubuntu machine.

1. Download GSL:

    https://www.gnu.org/software/gsl/

2. It will be a compressed tar file, like gsl-x.x.tar.gz

3. Place this file in the home 

4. Open the terminal and unpack this downloaded file:

    $ tar -zxvf gsl-x.x.tar.gz

5. A new folder will be created. cd to that folder.

6. Now we will configure, make and install GSL:

    $ ./configure
   $ make
   $ sudo make install

Each of these steps may take some time. Wait patiently. 
7. Close the terminal and open .bashrc file in the home. It is a hidden file. If required use Ctrl+H to access it. 
Edit this file to add:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
8. Save the edited file and close it.
9. Now get back to the terminal and compile a C code that uses GSL. For example, use the test code testgsl.c:

    #include <stdio.h>
    #include <gsl/gsl_sf_bessel.h>
    int
    main (void)
    {
        double x = 5.0;
        double y = gsl_sf_bessel_J0 (x);
        printf ("J0(%g) = %.18e\n", x, y);
        return 0;
    }

To compile:

    $ gcc -Wall testgsl.c -o run -lgsl -lgslcblas -lm
10. Now run the compiled code:

         $ ./testgslrun

This should print:

    $ J0(5) = -1.775967713143382900e-001 


Done!! Happy Computing. 


#C #GNU #GSL #computing #programming





How to install Compiler for C and GNU Scientific Library on Windows?

I often find that students are struggling to install a compiler for C and GNU Scientific Library (GSL) on their MS Windows machines. Sometimes Windows can behave weirdly while you try to install these tools. This happens mostly when you have installed and uninstalled lots of programs and packages. 

Here is a step-by-step guide to install 1) a compiler for C and 2) GSL on your Windows machine.

Installation of MinGW on Windows 10

1. MinGW is a native Windows port of the GNU Compiler Collection (GCC). Download the MinGW installer from SourceForge:

    https://sourceforge.net/projects/mingw/

2. Run the installer and follow the instructions on the screen.

3. The installer will install the MinGW installation manager. This is used for installing MinGW and to uninstall MinGW.

4. Run the installation manager. To start with we will install the basic setup.

    Select basic setup on the left-hand pane. 

    On the righthand pane for the package list, select each package one by one and mark those for installation (Right-click > Mark for Installation).

    Once all the packages are marked, install those: Installation > Apply Changes

5. Installation may take some time. Note that apart from MinGW, this process will also install MSYS. MSYS is a bash shell that we will use while compiling and running C codes.

6. On successful installation you may have the following directory structure:

    C:\MinGW

    Within this folder, you will find the MSYS subfolder. Find the msys.bat file.     For example, this may be at: C:\MinGW\msys\1.0\msys.bat

    Run this batch file (double-click) to get the bash shell. You can create a shortcut for this batch file and keep it at a suitable location, like the desktop.

7. Check MinGW installation by running a simple C code. For example, here is a 

code named as test.c

        #include <stdio.h>

        int main() {

                       printf("Hello, world!\n");

                       return 0;

        }

8.  To compile the code, you have to be in the folder with the code. Use cd to reach the appropriate folder. Note that we are now working in Bash shell (not in the Windows default command prompt). Therefore, in the pathname, we have to use / in place of \

9.  Compile the code: 

       gcc test.c -o testrun

10.  Run the code

       ./testrun

        If everything works fine, the program will print:

          $  Hello, world!


Installation of GSL on Windows 10

1. You must install MinGW with MSYS before installing GSL.

2. Download GSL:

    https://www.gnu.org/software/gsl/

3. Place the downloaded compressed file (named like: gsl-latest.tar.gz) in the MinGW folder. 

4. Start MSYS. We will now use MSYS for all the subsequent work.

5. cd to the appropriate folder and extract the files:

    $ tar xzf gsl-latest.tar.gz

6. The files will be placed in a newly created folder, for example, gsl-2.6 

7. Move to that newly created folder:

   cd gsl-2.6

8.  Configure, make, and install GSL:

   $ ./configure

  $ make

  $ make install

Each of these steps takes some time. Wait for the completion of each step before executing the next. 

9.  Upon completion of installation you should find the GSL lib and include folders inside msys folder. For example:

    C:\MinGW\msys\1.0\local\include

    C:\MinGW\msys\1.0\local\lib

10.  Compile and run a C code that uses GSL. Again, we will use MSYS to compile and run the code. For example, here is the code for testgsl.c:

    #include <stdio.h>

    #include <gsl/gsl_sf_bessel.h>

    int

    main (void)

    {

        double x = 5.0;

        double y = gsl_sf_bessel_J0 (x);

        printf ("J0(%g) = %.18e\n", x, y);

        return 0;

    }

11.   To compile:

    $ gcc -I/usr/local/include -L/usr/local/lib   testgsl.c -lgsl -o testgslrun


(Note that depending upon how MinGW and GSL have been installed, we may not have to include -I/usr/local/include -L/usr/local/lib in the command for compilation.)

12.   Run the compiled code:

         $ ./testgslrun

This should print:

    $ J0(5) = -1.775967713143382900e-001 

Done!! Happy Computing. 


#C #programming #GSL #computing #compiler #gcc