cversioningpari

How to print libpari version in c?


I would like to print version of libpari library from c program

#include <gmp.h>
#include <mpfr.h>
#include <mpc.h>
#include<pari/pari.h> 
int main(void){  
  
  gmp_printf ("MPFR library: %-12s\nMPFR header:  %s (based on %d.%d.%d)\n",
          mpfr_get_version (), MPFR_VERSION_STRING, MPFR_VERSION_MAJOR,
          MPFR_VERSION_MINOR, MPFR_VERSION_PATCHLEVEL);
  
  gmp_printf (" GMP-%s \n ", gmp_version );
  
  mpfr_printf(" MPC-%s \nMPFR-%s \n GMP-%s \n", MPC_VERSION_STRING, mpfr_version, gmp_version );
  
  gmp_printf("pari_version = %s\n",  GENtostr(pari_version())); //  paricfg_version_code);
  

return 0;

Program compiles but

gcc g.c -lpari -lmpc -lmpfr -lgmp -Wall
a@zalman:~/Dokumenty/gmp$ ./a.out
MPFR library: 4.1.0       
MPFR header:  4.1.0 (based on 4.1.0)
 GMP-6.2.1 
  MPC-1.2.0 
MPFR-4.1.0 
 GMP-6.2.1 
Memory protection violation

I have tried also

pari_printf("pari_version = %Ps\n",  GENtostr(pari_version())); //  paricfg_version_code);
  pari_flush();

I have googled it and search the manual. How can I do it ?


Solution

    1. The reason you get a memory protection violation in the first attempt is because your program is lacking a pari_init() call; the function pari_version() needs the pari stack to create its return value there...

    It should work in your main program, the one that actually uses the pari library and includes a pari_init() first. You might want to print the result as

        GEN v = pari_version(), M = gel(v,1), m = gel(v,2), p = gel(v,3);
        printf("pari-%ld.%ld.%ld", itos(M), itos(m), itos(p));
    

    (M, m, p stand for Major version number, minor version number and patchlevel)

    1. The second attempt using pari_printf() can't work for the same reason and also because you shouldn't then use GENtostr() because it converts the GEN to a char* and the %Ps construct expects a GEN argument.

    2. If you really want it to work without the pari stack, you can adapt the solution in your answer by "decoding" the version code as follows

      const ulong mask = (1<<PARI_VERSION_SHIFT)-1;
      ulong n = paricfg_version_code, M, m, p;
      p = n & mask; n >>= PARI_VERSION_SHIFT;
      m = n & mask; n >>= PARI_VERSION_SHIFT;
      M = n;    
      printf("pari-%lu.%lu.%lu", M, m, p);