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:

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

    I hope it helps.