Wednesday, December 8, 2021

Creating and using a dynamic library from JPL toolkit

Generating a dynamic library

In a previous post I showed how to generate a dynamic library from jpl toolkit, for linux systems (libcspice.so) and for Windows (libcspice.dll).

Before we proceed to the compilation of the library in the Termux environment, let's talk of the programs that we have just compiled from sources launching the script importCSpice.csh.

Open Termux in your phone and activate ssh:

sshd (and press Enter, from now on I take it for granted)

In your PC open your terminal emulator (cmd on Windows, gnome-terminal or whatever in Linux) and type:

ssh u0_a119@192.168.1.120 -p 8022 (use your own coordinates, this is an example, see previous post)

when you are in, type:

~ $ ls -l
total 107956
drwx------ 9 u0_a119 u0_a119      4096 Apr  8  2017 cspice
-rw------- 1 u0_a119 u0_a119 110530560 Apr 10  2017 cspice.tar
-rw------- 1 u0_a119 u0_a119        84 Apr 10  2017 importCSpice.csh
drwx------ 2 u0_a119 u0_a119      4096 Dec  6 19:26 storage
~ $

the directory cspice has been created during the compilation process

it contains several subdirs:

~ $ ls cspice
data  doc  etc  exe  include  lib  makeall.csh  src
~ $

let's look inside cspice/exe

 $ ls cspice/exe
brief        ckbrief.exe   dskexp       inspekt.exe  msopck      spacit.exe    states      tictoc.exe  version
brief.exe    commnt        dskexp.exe   mkdsk        msopck.exe  spkdiff       states.exe  tobin       version.exe
chronos      commnt.exe    frmdiff      mkdsk.exe    simple      spkdiff.exe   subpt       tobin.exe
chronos.exe  dskbrief      frmdiff.exe  mkspk        simple.exe  spkmerge      subpt.exe   toxfr
ckbrief      dskbrief.exe  inspekt      mkspk.exe    spacit      spkmerge.exe  tictoc      toxfr.exe
~ $

all of these files are executables, but built for different systems. The ones without extension are used in aarch64 architecture, those ending with .exe are for Windows.

You can personally check the nature of these executables, but first you must install the package file:

pkg install file

now change to subdirectory cspice/exe:

cd cspice/exe

and type file brief

~/cspice/exe $ file brief
brief: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, not stripped
~/cspice/exe $

this executable is one of our interest. We'll make a good use of it later. If you type file brief.exe instead:

~/cspice/exe $ file brief.exe
brief.exe: PE32+ executable (console) x86-64, for MS Windows
~/cspice/exe $

this exe file won't run in your phone, it's generated for cgywin, which is a posix compatible programming environment for Windows systems.

Just for curiosity, launch brief:

~/cspice/exe $ ./brief
WARNING: linker: /data/data/com.termux/files/home/cspice/exe/brief: unsupported flags DT_FLAGS_1=0x8000001

BRIEF -- Version 4.0.0, September 8, 2010 -- Toolkit Version N0066

   BRIEF is a command-line utility program that displays a summary for
   one or more binary SPK or binary PCK files. The program usage is:

      % brief [-options] file [file ...]

   The most useful options are shown below. For the complete set of
   options, run BRIEF with the -h option. The order of options is not
   significant. The case of option keys is significant: they must be
   lowercase as shown below.

      -c           display centers of motion/relative-to frames
      -t           display summary in a tabular format
      -a           treat all files as a single file
      -utc         display times in UTC calendar date format (needs LSK)
      -utcdoy      display times in UTC day-of-year format (needs LSK)
      -etsec       display times as ET seconds past J2000

   An LSK file must be provided on the command line to display times in
   UTC formats. FK file(s) must be provided on the command line to
   display names of any frames that are not built into the Toolkit.

~/cspice/exe $

Let's proceed to the creation of a dynamic library.

Change directory to cspice/src/cspice

~/cspice/src $ cd
~ $ cd cspice/src/cspice
~/.../src/cspice $ ls
F77_aloc.c     daftb.c     et2lst_c.c         lnknxt.c      rav2xf_c.c  spkr15.c       vnorm_c.c     zzeknres.c
SpiceCK.h      dafus_c.c   et2utc.c           lnkprv.c      rawio.h     spkr17.c       vnormg.c      zzeknrml.c
SpiceCel.h     dafwcr.c    et2utc_c.c         lnksiz.c      raxisa.c    spkr18.c       vnormg_c.c    zzekordc.c
... a lot of files ...
dafrfr_c.c     errint_c.c  lnkfsl.c           r_sqrt.c      spkr08.c    vlcomg_c.c     zzeklled.c
dafrrr.c       errprt.c    lnkhl.c            r_tan.c       spkr09.c    vminug.c       zzekllei.c
dafrs_c.c      errprt_c.c  lnkila.c           r_tanh.c      spkr10.c    vminug_c.c     zzeklltc.c
dafrwa.c       esrchc.c    lnkilb.c           radrec.c      spkr12.c    vminus.c       zzeklltd.c
dafrwd.c       esrchc_c.c  lnkini.c           radrec_c.c    spkr13.c    vminus_c.c     zzekllti.c
daft2b.c       et2lst.c    lnknfn.c           rav2xf.c      spkr14.c    vnorm.c        zzekmloc.c
~/.../src/cspice $

It's a huge amount of C sources, we need to compile them in a standard .so dynamic library.

within the same directory type:

gcc -Wall -O2 -fPIC -shared -o libcspice.so *.c -lm -ldl

You deserve a brief explanation of the options I have given to the compiler:

gcc is the compiler, here used as assembler, compiler and linker altogether, any further consideration is beyond our scope.

-Wall stands for the maximum warning alert level, during compilation you'll see a ton of warnings.

-O2 is the optimization level, set to a good level compatible with robustness.

-fPIC -shared is the combination of two instructions aimed at generating a shared library with Position Independent Code (PIC)

-o libcspice.so is the name I have choosen for the compiled library, "lib" and ".so" are the structural parts.

*.c are all the C sources that are present in this directory

-lm -ldl are linked libraries, math and dynamic loader

After some time and a big amount of warnings, hopefully no errors found, you'll see a new file in your directory, libcspice.so. Move it to your home directory, to have it handy

mv libcspice.so ~/

Ok, it's enough for now, see you in the next post

No comments:

Post a Comment

How to create a virtual linux machine with qemu under Debian or Ubuntu with near native graphics performance

It's been a long time since my latest post. I know, I'm lazy. But every now and then I like to publish something that other people c...