Re: [Vala] Problem with math functions



I had to compile with

    valac TPK.vala -X -lm

that will tell the linker to use the math library. I don't know why your other example works.

regards
alberto
________________________________________
From: vala-list-bounces gnome org [vala-list-bounces gnome org] On Behalf Of Carlos Costa [ccosta bsd com br]
Sent: 17 July 2009 20:47
To: vala-list gnome org
Subject: [Vala] Problem with math functions

Hi guys,

Thanks by your attention to my previous e-mail. That www.valadoc.org
link is awesome =)

Now I'm passing by a compilation problem. Firstly, when I do the
example code below I don't have problems to compile with vala 0.7.4:

using GLib;

public static void main (string[] args) {

  stdout.printf("%f\n",Math.sqrt(81.0));

}

But when I tryed to compile a little more complex example, the TPK
Algorithm below, the valac command reports a error undefined reference
to sqrt:

/* Knuth's TPK algorithm in the vala programming language */

using GLib; // Because we need GLib.Math

public class TPK : Object {

  /* f(x) = sqrt(|x|) + 5*x**3 */
  static double f (double x){
    return Math.sqrt(Math.fabs(x)) + 5.0 * Math.pow(x,3.0);
  }

  public static int main (string[] args) {
    double[] A = new double[11];
    double y;

    /* Read in the values of the array A */
    for (int i = 0; i <= A.length; i++){
      A[i] = stdin.getc();
    }

    /* In reverse order, apply "f" to each element of A and print */
    for (int i = 10; i >=0; i--) {
      y = f(A[i]);
      if (y > 400.0){
        stdout.printf("%d TOO LARGE\n", i);
      }
      else {
        stdout.printf("%d %f\n", i, y);
      }
    }

    return 0;
  }
}

The valac error output is:

$ valac tpk.vala
/tmp/ccUbEj6Z.o: In function `tpk_f':
tpk.vala.c:(.text+0x35): undefined reference to `sqrt'
tpk.vala.c:(.text+0x53): undefined reference to `pow'
collect2: ld returned 1 exit status
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)

Why the compiler is reporting this error? I don't see any obvious
error in my code, but I'm a beginner in vala and it can be a
trap/pitfall of the language. I hope you should clarify it.

Thanks in advance.

--
Carlos
_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list

This email has been scanned for all viruses by the MessageLabs Email
Security System.

This email has been scanned for all viruses by the MessageLabs Email
Security System.



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