0

I am trying to call a C function (statslib statistical library) from vala file. My main.vala file is as follows:

extern double sum(double[] doublelist, int len); 
public static void main(string[] args){
    double[] numlist = {1.1,2.2,3.3,4.4,5.5};
    var total = sum(numlist, numlist.length); 
    stdout.printf("The sum is %f \n", total);

However, on trying to compile with C file, I get following error:

$ valac main.vala statlib.c 
/usr/bin/ld: /tmp/ccjchX8B.o: in function `g_mean':
statlib.c:(.text+0x35a): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x399): undefined reference to `exp'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `agm':
statlib.c:(.text+0x6db): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `s_stdev':
statlib.c:(.text+0xb8a): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `p_stdev':
statlib.c:(.text+0xbc5): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `central_moment':
statlib.c:(.text+0xd47): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `std_err_mean':
statlib.c:(.text+0xdbd): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `rms':
statlib.c:(.text+0xe6a): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `percentile':
statlib.c:(.text+0xfa7): undefined reference to `floor'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `skewness1':
statlib.c:(.text+0x12bc): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `skewness2':
statlib.c:(.text+0x1379): undefined reference to `pow'
/usr/bin/ld: statlib.c:(.text+0x13c9): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `kurtosis1':
statlib.c:(.text+0x152b): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `kurtosis2':
statlib.c:(.text+0x1656): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o:statlib.c:(.text+0x16a6): more undefined references to `pow' follow
/usr/bin/ld: /tmp/ccjchX8B.o: in function `corr_coeff':
statlib.c:(.text+0x18fe): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `stdev_points':
statlib.c:(.text+0x1c82): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `stderr_reg_coeff':
statlib.c:(.text+0x1d8b): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `fisher':
statlib.c:(.text+0x1f38): undefined reference to `log'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `binom_prob':
statlib.c:(.text+0x20ba): undefined reference to `pow'
/usr/bin/ld: statlib.c:(.text+0x20ed): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `stdx_lg':
statlib.c:(.text+0x2178): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `norm_pdf':
statlib.c:(.text+0x221e): undefined reference to `exp'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `std_ncurve_area':
statlib.c:(.text+0x22ff): undefined reference to `exp'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `anderson_darling_norm':
statlib.c:(.text+0x278e): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x27f7): undefined reference to `log'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `rndz':
statlib.c:(.text+0x2979): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `log_norm_pdf':
statlib.c:(.text+0x29fc): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x2a1e): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x2a6e): undefined reference to `exp'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `gamma':
statlib.c:(.text+0x2b49): undefined reference to `pow'
/usr/bin/ld: statlib.c:(.text+0x2be1): undefined reference to `pow'
/usr/bin/ld: statlib.c:(.text+0x2c16): undefined reference to `exp'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `log_gamma':
statlib.c:(.text+0x2d0e): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x2d2d): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x2d64): undefined reference to `log'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `std_betapdf':
statlib.c:(.text+0x2e51): undefined reference to `pow'
/usr/bin/ld: statlib.c:(.text+0x2e8f): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `betapdf':
statlib.c:(.text+0x2faa): undefined reference to `pow'
/usr/bin/ld: statlib.c:(.text+0x2ff3): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `beta_curve_area':
statlib.c:(.text+0x311d): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o:statlib.c:(.text+0x3166): more undefined references to `pow' follow
/usr/bin/ld: /tmp/ccjchX8B.o: in function `tdist':
statlib.c:(.text+0x324d): undefined reference to `sqrt'
/usr/bin/ld: statlib.c:(.text+0x32ed): undefined reference to `pow'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `t_test2':
statlib.c:(.text+0x348e): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `t_test1':
statlib.c:(.text+0x3512): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `confidence_95':
statlib.c:(.text+0x3600): undefined reference to `sqrt'
/usr/bin/ld: statlib.c:(.text+0x363e): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o: in function `confidence_99':
statlib.c:(.text+0x3704): undefined reference to `sqrt'
/usr/bin/ld: /tmp/ccjchX8B.o:statlib.c:(.text+0x3742): more undefined references to `sqrt' follow
/usr/bin/ld: /tmp/ccjchX8B.o: in function `root':
statlib.c:(.text+0x37fc): undefined reference to `log'
/usr/bin/ld: statlib.c:(.text+0x3822): undefined reference to `exp'
collect2: error: ld returned 1 exit status
error: cc exited with status 256
Compilation failed: 1 error(s), 0 warning(s)

If I remove all functions (except sum) from statlib.c file, the command works perfectly and creates an executable file.

How can I avoid getting above errors without changing statlib.c file?

rnso
  • 23,686
  • 25
  • 112
  • 234

1 Answers1

1

You need to tell the compiler that you want to link math library

Add -lm to your command line

https://godbolt.org/z/dPn9b7rrs

enter image description here

0___________
  • 60,014
  • 4
  • 34
  • 74