Search code examples
c++visual-studio-codegsl

Trouble printing from C++ file in VS Code when file uses a GSL function


I am trying to run an example script in C++ using GSL functions. I am using Visual Studio Code to compile and execute the program. The example code is below (I saved it under the file name gsl_bessel_example.cpp) and I got it directly from the GSL 2.7 Scientific Library documentation here.

#include <stdio.h>
#include <gsl\gsl_sf_bessel.h>

int
main (void)
{
  double x = 5.0;
  double y = gsl_sf_bessel_J0 (x);
  //double y = 10.0;
  printf ("J0(%g) = %.18e\n", x, y);
  return 0;
}

I expect the program to output the following to the terminal after I have compiled and executed the program:

J0(5) = -1.775967713143382642e-01

When I compile the program, I use the following in the command line:

g++ -Wall gsl_bessel_example.cpp -IC:\msys64\mingw\include -LC:\msys64\mingw\lib -lgsl -lgslcblas

and execute it with:

.\a.exe

There are no errors or warnings given when I compile the file -- it compiles fine. However, I do not get any output when I execute it. If I comment out the line:

double y = gsl_sf_bessel_J0 (x); 

and use:

double y = 10;

instead then recompile and execute, I receive an output I expect (see attached images for the results). Does anyone know why the file will not execute as expected when I use the GSL function? I have tried this for other examples provided in the GSL documentation but have similar results for all (no output).

Image of Code and Output Using GSL Function

Image of Code and Output Not Using GSL Function


Solution

  • This answer might be a bit late but I encountered the same problem and thought I'd share my solution.

    Problem: the compiled binary cannot find the linked libgsl.dll and libgslcblas.dll libraries

    Solution:

    • Add the <gsl-path>\gsl\bin path to the PATH environment variable
    • Restart all terminals

    Here are the steps I took to find what the issue was:

    • I compiled the source both with 32-bit gcc/GSL (installed from here) and 64-bit gcc/GSL (installed with vcpkg).
    • I ran the executable in WSL, and it gave me an error of "libgsl.dll not found".
    • I placed the libgsl.dll and libgslblas.dll files found in <gsl-path>\ gsl\bin in the same directory of the .exe and it worked
    • I checked if I had not added the gsl\bin folder to PATH environment variable
    • It was already added, so I had to restart all terminals to make it work.

    I hope it helps.