[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] how GSL works?

From: Warren Weckesser
Subject: Re: [Help-gsl] how GSL works?
Date: Thu, 22 May 2008 16:16:43 -0400


There are a few problems with your code, including on big one.

Your "include" statements for gsl are not correct.  Do not include
the full path to a library file.  Instead, use this:

#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>

To avoid warnings, main() should be declared "int", and should return
an integer.

The %f specification in the printf function is for a real number, not
a complex number.

Here is a modified version of your program example.c:
#include <stdio.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>

int main()
    gsl_complex z,r, sum;
    double x,y;

    x= 3;
    y= 4;
    z = gsl_complex_rect (x,y);
    r = gsl_complex_rect (y,x);
    sum = gsl_complex_add (z,r);
    printf ("The sum is: %f + %fi\n", GSL_REAL(sum),GSL_IMAG(sum));
    return 0;

There are several ways you can give the commands to compile this
program.  I'll assume you are using gcc.

My preference is to use the pkg-config command.  If you are using
a system that has the pkg-config command installed, you can do this:

$ gcc -Wall example.c $(pkg-config --cflags --libs gsl) -o example

to compile the program.  The option "-Wall" is not required, but
it is a good idea to use it.  The option "-o example" puts the
executable file in "example".

Here is how I run the program, and the output:

$ ./example
The sum is: 7.000000 + 7.000000i

[Note: instead of the option
    $(pkg-config --cflags --libs gsl)
you can use
    `pkg-config --cflags --libs gsl`
but the back-ticks in that version are often confused with regular
single quotes, and I have been told that back-ticks are not easily
generated on some commonly used keyboards in Europe.]

If you prefer to compile the code to an object file, and then
link separately, you can do this:

$ gcc -c -Wall example.c $(pkg-config --cflags gsl)
$ gcc example.o $(pkg-config --libs gsl) -o example

To see an example of how to compile and link a simple program
without using pkg-config, read sections 2.1, 2.2 and 2.2.1 of the


On Thu, May 22, 2008 at 7:06 AM, Ayoze Amaro García <
address@hidden> wrote:

>  Good day,
>  A few days ago I discovered the gsl, wich was aperfecto option for my
> classes, but I trying to doy a very simple program with complex numbers:
> creating, add, sub, and so on, but I can not neither compile nor execute
> because it seems that I am not linking well or something like that. I send
> my example code reduced tu just make rect numbers and add them after, to
> print  the result on screen at the end.
>  Thanks in advance.
> Ayoze Amaro García
> _______________________________________________
> Help-gsl mailing list
> address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]