Coding using C on the Onion Omega
-
Looked some more into this. I'm not sure I understand the logic here.
When I execute
opkg install libonionspi
it copies libonionspi.so to /usr/lib/. This is the dynamic library which the linker looks to when we rungcc main.c -lonionspi
. What I don't understand is, where's the header-file? They tell us to #include <onion-spi.h> (https://wiki.onion.io/Documentation/Libraries/SPI_Library_C).As seen above I found those files on https://github.com/OnionIoT, but they're not updated (missing some functions).
Clearly I must be missing something vital here. I don't understand why I can't develop stuff on the Onion Omega itself. 8GB of hdd-space, 64MB RAM and I could add swap space if memory ever became a problem.
Still trying to avoid cross-compilation..
-
@huxflux
You could symlink your /usr/lib headers into whatever system header/compilation header libraries gcc on the device is expecting. Of course, you could also try to directly #include to the /usr/lib folder (NOT RECOMMENDED, i don't know why, but i'm pretty sure this is frowned upon by some neck-beard somewhere)In any case, you could also be dealing with the fact that the OnionIOT headers are exported kernel headers, which may or may not work if they are stripped of something the kernel headers might have implicitly expected. But now we're descending into the madness of kernel vs exported headers, and I don't want to go down that path.
In any case, the cross-compilation toolchains aren't there because these embedded devices can't do it, so much as it ensures that if you can set up a cross-compilation toolchain that you can ensure OTHERS can develop just as you do for the same expected target. Embedded systems long ago (really since breaking the GHz mark) could handle on device compilation. However, it would take a while and development cycles are generally precious, therefore, most prefer to run it on a beefy machine.
That notwithstanding, I do think that there are more and more merits to on device compilation nowadays. My biggest wish is to run a linux system on a beefy but tiny AP like Onion Omega, and then connect to a virtual IDE hosted within it to actually develop from a pristine iPad or the like. This would make it so that I CAN have linux IDEs while using my sexy idevice.
Hope this helps. I'll look around to see what the latest is with on device development and see if I can't get you a more clear answer. Unless linux is built on the system itself, I don't have the faintest idea how to get development environments built without cross compilation though. Bitbake and openwrt are the best solutions to cross compilation, in that order, and even they eschew on device development.
-
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!