Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: sammyMaX on February 24, 2012, 02:20:42 pm

Title: Noob Questions about Porting
Post by: sammyMaX on February 24, 2012, 02:20:42 pm
I would like to use GMP (http://gmplib.org/) in an Nspire program. I've experimented with building it a bit, but I really don't know what I am doing. Hopefully these questions don't sound too stupid:
1. If I compile a library and use it in my program, and build my program using nspire-gcc, would the library have to be in a special format so it can be used on Nspires?

2. What do libraries even compile into (in Linux)? I would think that if you included the header file of a library, then when you build your own project, the compiler will see the other files from the library included in it, and compile those as well.

3. If GMP compiles, would it even be in a form that is usable for Nspire applications?
Title: Re: Noob Questions about Porting
Post by: Lionel Debroux on February 25, 2012, 03:58:39 am
There are two main kinds of libraries: static libraries (in *nix, usually ".a" files; in Windows, often ".lib" files), used at compile time, and dynamic libraries (".so" in *nix, ".dll" in Windows), used at runtime.
Sometimes, for using a dynamic library, a small static library containing imports for the dynamic library is linked statically with a program.

There's no support for dynamic libraries in Ndless (yet), so you would have to make a static build of GMP, and then link another program against that static library: the code of the GMP functions you use, and their transitive dependencies, would be embedded into your program (and for the common subset, copied across multiple different programs which use different sets of functions from said static library).
Title: Re: Noob Questions about Porting
Post by: sammyMaX on February 25, 2012, 10:36:03 am
In order to use GMP in my code, would I just say:
Code: [Select]
#include <gmp.h> // Would the compiler find the library it points to and use it?
And I would still have to port GMP right? (I can't just use the GMP already built for Linux because it's not for the right architecture?)
       If I do have to port GMP, I would have both an i386 and an ARM version, so how would the compiler know which version to use?
Title: Re: Noob Questions about Porting
Post by: Lionel Debroux on February 25, 2012, 11:15:51 am
* in your C source code itself, indeed, you would just say "#include <gmp.h>";
* you'd also have to add a "-lgmp" flag to the linker flags in your Makefile (or whatever you use for triggering the build);
* you should add a -L flag too, to tell the linker where to find the "libgmp.a" library you've compiled for the current ARM platform.


I've just made and tested a quick script for compiling the GMP static library I mentioned above:
Code: [Select]
#!/bin/sh
# This creates a "gmp" subdirectory whereever you launch the script, and compiles and installs in subdirectories thereof.
mkdir gmp
cd gmp
WORKING_DIR=`pwd`
wget ftp://ftp.gmplib.org/pub/gmp-5.0.4/gmp-5.0.4.tar.bz2 || exit 1
tar xvjf gmp-5.0.4.tar.bz2 || exit 1
mv gmp-5.0.4 src
mkdir build
mkdir prefix
cd build
../src/configure --host=arm-none-eabi --prefix="$WORKING_DIR/prefix" --enable-shared=no --enable-assert || exit 1
make && make install

Upon successful execution of the script, in the "prefix" subdirectory, you have include/gmp.h and lib/libgmp.a, so you'd want a -L<whereever>/prefix/lib linker flag :)

NOTE: when cross-compiling, it's hard to run "make check" and verify the compilation. This script doesn't tackle the problem.

[EDIT: clarified a sentence.]
Title: Re: Noob Questions about Porting
Post by: sammyMaX on February 25, 2012, 03:44:53 pm
It compiles :)

I tried making a program, and it can never find gmp.h. I modified the Hello World sample a bit, and the only line I changed was LDFLAGS, which I changed to this:
Code: [Select]
LDFLAGS = -L /home/philip/gmp/prefix/ -lgmp -nostdlibAnd the compiler tells me that it can't find gmp.h. Are the -L and -lgmp flags supposed to be in GCCFLAGS, or LDFLAGS?
Title: Re: Noob Questions about Porting
Post by: ExtendeD on February 25, 2012, 04:54:35 pm
-L and -l should be in LDFLAGS.
CFLAGS should contain -I /path/to/the/include/directory, that contains gmp.h.
Title: Re: Noob Questions about Porting
Post by: sammyMaX on February 25, 2012, 06:25:42 pm
It finds the library and header now, but gives off these errors:
Code: [Select]
osstub.c:6:18: error: conflicting types for ‘_ssize_t’
/home/philip/lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/include/sys/_types.h:56:13: note: previous declaration of ‘_ssize_t’ was here
osstub.c:8:18: error: conflicting types for ‘_off_t’
/home/philip/lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/include/sys/_types.h:16:14: note: previous declaration of ‘_off_t’ was here
make[1]: *** [osstub.o] Error 1
make: *** [hello.tns] Error 2
What are _ssize_t and _off_t?

Edit: I don't think these errors are related to GMP at all - compiling the normal Hello.c gives me the same errors. Any help?
Title: Re: Noob Questions about Porting
Post by: Lionel Debroux on February 26, 2012, 01:55:49 am
Upgrade your Ndless version to the latest version :)


Off-topic: I know that you're not making an Lua project, but this topic somehow made me wonder whether there are Lua bindings for GMP... and I found one:  http://www.dkrogers.com/doug/lua/ . But it was made years ago, so if it is to be ported to the Nspire, it would require quite a bit of work, potentially on the Lua binding side and definitely on the GMP side.
Combining that, with the support for native Lua extensions of the latest Ndless SVN versions (Omnimaga news (http://ourl.ca/15233) / TI-Planet news (http://tiplanet.org/forum/viewtopic.php?f=43&t=8807)), could showcase an interesting cooperation between Lua and native code. This is exactly the sort of thing that would benefit both users and TI, but TI will certainly kill it in ~10 days alongside the current iteration of Ndless in ~10 days, notwithstanding our freedom to tinker.
Title: Re: Noob Questions about Porting
Post by: sammyMaX on February 26, 2012, 11:23:23 am
Lua and GMP would be interesting, and it's a shame that TI doesn't open up to third-party development, because it could help them tremendously...

Anyways, I try to build my little program and it now gives me these errors:
Code: [Select]
hello.o: In function `main':
hello.c:(.text.startup+0x18): undefined reference to `__gmpz_init'
hello.c:(.text.startup+0x2c): undefined reference to `__gmpz_set_str'
hello.c:(.text.startup+0x34): undefined reference to `__gmpz_init'
hello.c:(.text.startup+0x44): undefined reference to `__gmpz_pow_ui'
hello.c:(.text.startup+0x54): undefined reference to `__gmp_printf'
collect2: ld returned 1 exit status
make: *** [hello.tns] Error 1
Could this be because it is trying to use the wrong version of GMP (an i386 one) or is it still a problem in my makefile?
Here are my builder and linker flags:
Code: [Select]
GCCFLAGS = -I/home/philip/gmp/prefix/include -Os -nostdlib -Wall -W -marm
LDFLAGS = -L/home/philip/gmp/prefix/lib -lgmp -nostdlib