Coding using C on the Onion Omega
-
Thanks for replying!
We already can compile on the Onion Omega using the
python-light
package. If you add some more space through USB, you can even installgcc
on the Onion Omega. There are no problems whatsoever using Vim and compile C-code on this unit. The problem is when I want to use their librarieslibonionspi
and/orlibonioni2c
. For some reason, they don't include the header-files when you installlibonionspi
and/orlibonioni2c
.The reason I want to code directly on the Onion Omega is because it's fast. I just want to write small programs that deal with the LEDS or read/write stuff to the GPIO's.
I could of course go without their libraries and go low-level myself. I might do it. Or maybe I should just go with Python. Or maybe go the cross-compilation route. We'll see
-
@huxflux When you install a library (like
libonionspi
and/orlibonioni2c
) all that is installed is the compiled and linked code of the relevant library - it does not include any of the sources - in particular the*.h
files needed to reference the libraries. You need to ensure that the*.h
files are accessible when you build your program - either put them in the same directory as your source file or use the-I<directory>
option to point to the include files. If you do this, you should not need to separately and additionally compile the*.c
files for the library.
-
@huxflux Look at the cloud it just came out, it gives you the ability to compile c or c++ programs for your omega.
-
@Chris-McCaslin Not yet it doesn't as far as I can see. Though according to https://community.onion.io/topic/677/the-onion-roadmap it should be available early this month (May)
-
@Kit Bishop I tried compiling using only the .h-files with
gcc main.c -loniondebug -lonionspi
. The linker complains about not finding the reference to one of the functions. If I editlibonionspi.so
I can see the name of the function is there. So I'm at loss to what's going on here. It compiles, but doesn't link.
-
@huxflux Sorry not to be of any more help. I haven't tried compiling on the Omega itself. My earlier comments were just possible suggestions
-
@huxflux Ahhhh, I see.
Is https://github.com/OnionIoT/spi-gpio-driver/blob/master/include/onion-spi.h what you are looking for then? I think if you download the files in this repo, and they are in somewhere like "/usr/include", you would then be able to satisfy the
#include <onion-spi.h>
dependency, right?
-
@Lazar-Demin @Boken-Lin , can you all update https://wiki.onion.io/Documentation/Libraries/SPI_Library_C to include where to get and place the required spi files for this tutorial? Perhaps also ensure that the repo providing the spi driver for C code is definitely up to date?
-
@Theodore-Borromeo All of the articles in the Library Documentation section have been updated to point to the GitHub repo where the source code can be found.
All of the repos are definitely up to date, that's where the source code for our firmware comes from!One more note:
Keep your eyes peeled in the next few weeks for the Cloud Compile feature to be released. You can avoid having GCC installed on your Omega and pulling code from all over the place. You'll be able to upload your source code, the cloud will cross-compile it for you, and then you will be able to choose which device you would like to push the compiled binary!
-
@Theodore-Borromeo Yes it fixes the dependency so I'm able to compile it, but not link it.
undefined reference
to one or more of the functions.Cloud Compile sounds fancy schmancy
-
@huxflux Does your linker command include the linking instructions from the wiki article?
Also, do you have the onion spi library installed?
Run:opkg update opkg install libonionspi
To install the library
-
@Lazar-Demin Yes, this is what I execute
gcc main.c -loniondebug -lonionspi
Here's the testfile for compiling http://pastebin.com/gj8QT1k1. I also have libonionspi.so and liboniondebug.so in /usr/lib/.
-
@huxflux What output do you get when you run that command?
Maybe try adding-L /usr/lib
so it knows where to look for the dynamic libraries?
-
@Lazar-Demin Check out post #4 in this thread. I'll try with
-L /usr/lib
Btw, minor detail. When I include onion-spi.h, that file also includes onion-debug.h. Which is not found at https://github.com/OnionIoT/spi-gpio-driver/ but found at https://github.com/OnionIoT/i2c-exp-driver
-
I now have onion-spi.h from https://github.com/OnionIoT/spi-gpio-driver/ and onion-debug.h from https://github.com/OnionIoT/i2c-exp-driver and moved them to /usr/include. I execute
gcc main.c -L /usr/lib -loniondebug -lonionspi
and get this errormessage: http://pastebin.com/2K3eX8x4Basically the same as post #4
-
@huxflux said:
spiParamInit
That is defined in: https://github.com/OnionIoT/spi-gpio-driver/blob/853a4ab5010cd23847e3b0ff87ff6b1bd1447187/src/onion-spi.c
soooooo, try to maybe include that in your build as well? perhaps something like:
gcc main.c onion-spi.c -loniondebug -lonionspi
or whatever. . .
however, the more i think about it, those libs should be providing the definition for what the headers declare. so the linking to the libs as well as pointing to usr/lib for the headers should just work. It seems that perhaps the libs work for omega folks as they have the headers working. Maybe their packages didn't properly export the headers upon library installation with opkg, and it works for them coincidentally?
-
so in light of all this, you're probably better off compiling it all by hand into your app yourself, if linking to the built libs aren't working. Of course, I'm hopeful that it is as simple as that and you don't need yet other libs. . .
-
@Theodore-Borromeo That is exactly what I'm thinking. They're not including the header-files in their
libonionspi
andliboniondebug
packages. But even when I've downloaded the headers from their github, they still don't work as I think they should. I'm no expert on these matters. Far from it.I did manage to do something though. This is all on the Onion Omega. I downloaded onion-debug.c, onion-spi.c, onion-debug.h and onion-spi.h from github. I moved onion-debug.h and onion-spi.h to /usr/include. I then executed these commands:
gcc -Wall -fPIC -c onion-spi.c onion-debug.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 onion-spi.o onion-debug.o
ln -sf libctest.so.1.0 libctest.so.1
ln -sf libctest.so.1.0 libctest.so
We just made a dynamic library out of onion-spi.c.
Next step is to make a simple code for trying out the functions in the library. Lets call it main.c
#include <stdio.h>
#include <onion-spi.h>int main(int argc, char *argv[])
{
struct spiParams params;
spiParamInit(¶ms);
return 0;
}gcc main.c -L/root/coding/spi -Wl,-rpath,/root/coding/spi -lctest
and voila! It works. Of course you'll have to change the paths to the correct places.It is possible to build dynamic libraries from their code. It's also possible to compile and link using those dynamic libraries. And all this can be done on the Onion Omega itself.
I'll try to see if I can put all these files into the correct directories and see if the compiling and linking gets easier. Eg.
gcc main.c -lonionspi
.EDIT: And that works out too. I can now compile with
gcc main.c -lonionspi
. Just change the name of libctest to libonionspi andmv libonionspi.so.1.0 /usr/lib/
. Then justcd /usr/lib
and make the symbolic links.Well, I did learn a thing or two from this experience. Worth!
-
So if that worked, there is no reason you couldn't use -rpath options to just point out the header files you needed to properly utilize the provided spi libs, right?
You went through a lot of trouble to prove that their libs are in fact built from the sources you already have access to (well, similar enough that it at least doesn't error out), but all I was trying to point out were ways to satisfy the linkage dependencies in such a way to make gcc 'see' that the provided libs do in fact contain the definitions of those functions declared in the associated headers you fetched from github. . .i.e., try doing the first solution in:
http://stackoverflow.com/questions/22426574/gcc-undefined-reference-toIn any case, I do think @Lazar-Demin and @Boken-Lin should ensure that the libraries do provide the exported headers so that the APIs work 'out of the box' I would be heartbroken if I have to figure out the 'hard' linkage flags necessary to do this.
Thankfully, we aren't needing to use prelinked tables for library loading such that you could even use the -fPIC option. However, once I see these types of options, my eyes tend to glaze over. I still don't get why developers need to know this much to make code 'go'. We should just insist on python already
-
@Theodore-Borromeo That's what I thought, but it's not the case. Using -rpath options together with their libonionspi.so did nothing. I'm not sure what's going on.
Anyway, I was doing this for fun, and to prove a point. It is possible to compile and link on the Onion Omega using gcc. If you have the diskspace, since gcc is about 22MB.
Cloud compiling is coming soon. No need for this stuff.