Compiling small C++ Program

Hi,

Is there a way I can compile a small C++ program in Arduino Linino unix ?

Does this has any C++ compiler?

Please provide some light on this.

Thanks in advance.

Regards

BREL

The arduino platform already uses C++ using the avr-gcc compiler. Why don't you post your small C++ program and let us see if what minor changes might be required and why the changes are needed.

Once you compile the linino distribution you have the toolchain, see:
http://playground.arduino.cc/Hardware/Yun#build_linino
Do make sure the C++ options are enabled in the make menuconfig step. This post may be of some help too:
http://forum.arduino.cc/index.php?PHPSESSID=6dvdadfe8fsa7vngqthbrelav7&topic=191613.msg1416145#msg1416145

I believe you can build only the toolchain but I built the whole enchilada so I can't say definitively.

I have seen reports on the forum that after the build bombs the first time you have the toolchain and can cross compile your own programs at that point. The process that builds the full image downloads all the sources from upstream locations and can fail for multiple reasons so it will likely bomb without coercion.

Hi NoblePepper,

Thank you very much for the reply,

All I need is a way to comple small c++ program in Linino. This C++ function is used to execute some avrdude commands. Please tell me what is the least path I can take for this.

Thanks in advance.

Regards

BREL

The post above is the simplest path that I KNOW will work. If you are on windows you will need to see the relevant parts in the build linino instructions.

Here is the official OpenWrt method:
http://wiki.openwrt.org/doc/devel/crosscompile

The process on the Playground page is basically the build root that the cross compile instructions refer to but with more details.

This precompiled version might work, I haven't used it:
http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

NoblePepper,

I am little confused here, ( sorry my understanding may be limited ). I just need C++ compiler in Linino and compile my c++ code. I am not understanding all this toolchain and etc..

Something like G++ or CC c++ compiler.
CC a.cpp ->>a.exe (compiling a.cpp program to a.exe file in Linino)
or G++ a.cpp --> a.exe
I was hoping opkg G++ or Avr-cCC to install the package

Please can you throw some light. why do I need all toolchain etc...
Thanks and Regards,

BREL

On linux systems a compiler by itself is relatively useless, you also need things like a preprocessor, assembler, linker, make utilities, libraries to link against, and a bunch of other stuff I don't know much about. All this together is what is called a toolchain and probably what you are calling a compiler. Plug "what is a toolchain" into google and you'll get lot's of info.

I thought you wanted to compile a program for Linino on your host system which is what I was describing. Compiling on a embedded system isn't commonly done, most people who suggest something similar get a response like "that's a stupid idea, you must be stupid if you want to do that!". I don't agree but it is a difficult task and has limited utility. The reasoning is that you have a computer with lots of speed, memory, storage etc to compile on and the processor in the Linino side of the Yun is a feeble little machine in comparison.

What is commonly done is "cross-compiling", you compile the program on a host system and down load and run it on a target system. This is what the Arduino IDE does and what the OpenWrt tool-chain does. While you would probably use the same host for both (your "big" computer) they have different targets. The Arduino target is the 32U4 which is an AVR architecture. The OpenWrt target is the AR9331 which is a MIPS architecture.

It is definitely possible to "native compile" but I haven't seen anyone doing it. There is one thread about cross compiling AVR code on Linino but it doesn't have details of how and I haven't yet duplicated fibasile's results-
http://forum.arduino.cc/index.php?topic=188307.msg1393757#msg1393757

You may want to use an interpreted language, several are available for the Yun, Python and Lua are installed on the Yun as you received it.

Hi NoblePepper,
Superbly said and my sincerest thanks to you, at the least you clarified many doubts which I was breaking my head into and going nowhere.

I am perfectly OK, compiling code on host machine and running it on Linino server. As long my little piece of C++ code runs in Linino and does the job . thats it. ( Sorry, I come from the background of compiling c++ code on Windows, AIX, Solaris, Linux etc.. separately in their environment. Never thought of cross compiling)

As you also mentioned, I am not hard-stoned to C++ program. If Python does this job I am perfectly OK with this. Intention here is to run some Linux and AVRDUDE command. Can this be done in Python? Please point me to some pointers here.

Again, Superbly said and Thank you

Kindest Regards,

BREL

Python is a powerful language, the Arduino/Linino people put together a lot of the Yun system with it. I am far from fluent in Python, most of the stuff in Python is over my head, I can't really tell you it's strengths and weaknesses.

You'll find a lot of examples of Python in the /usr/lib/python2.7 directory on the Yun.

You can also use shell scripts, there are a lot of those in /usr/bin, the run-avrdude script does things similar to what you are describing. The Yun uses busy box ash so if you are used to bash you may find some things missing.

The build-root environment isn't that difficult to set up, mainly frustrating as some packages will bomb, you just go back to the make menu-config step and disable the offending package. You may want to look at http://downloads.openwrt.org/docs/eclipse.pdf if you go that route, you get a full featured IDE with source level debugging and everything.

Hi NoblePepper,

Is WRT Eclipse is available on Windows or is only in Linux .

Looks like only Linux?

Can you please confirm. Thanks

BREL

OpenWrt requires a case sensitive file system so it will not work on Windows, the playground link on build-root talks about running linux in a virtual machine in Windows.

Eclipse itself is written in Java and according to their website works on virtually anything including
Windows 32-bit
Windows 64-bit
Mac OS X(Cocoa 32)
Mac OS X(Cocoa 64)
Linux 32-bit
Linux 64-bit

NoblePepper and All,
I would like to go ahead and start creating a new Linino Image on a clean Ubuntu machine.

One last thing, I want to ask; Is there a way,just to unpack the G++ Toolchain in Arduino Linino and use this for compiling without going through the whole build. And apart from following your steps, any general advice is highly appreciated.

Thanks and Regards

BREL

I seem to remember using this to compile some simple programs without trouble, anything with dependencies on the kernel version definitely won't work:

This precompiled version might work, I haven't used it:
http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

If the way someone else has done it is not exactly what you want there are certainly multiple ways to do almost anything, but you will need to figure them out. This is how I did it:

Once you compile the linino distribution you have the toolchain, see:
Arduino Playground - HomePage

The build-root environment isn't that difficult to set up, mainly frustrating as some packages will bomb, you just go back to the make menu-config step and disable the offending package.

I believe you can build only the toolchain but I built the whole enchilada so I can't say definitively.

I have seen reports on the forum that after the build bombs the first time you have the toolchain and can cross compile your own programs at that point. The process that builds the full image downloads all the sources from upstream locations and can fail for multiple reasons so it will likely bomb without coercion.

General advice #1 - Persistence in searching for the answer is the key to learning new things.
General advice #2 - Never pet a burning dog.

Thank you very much NoblePepper.

Regards

BREL

If you really really really (really) have to reflash the Yun, at least use the official documentation we just published. It's still dangerous, but chances are that you'll get things working http://arduino.cc/en/Tutorial/YunUBootReflash

NoblePepper,

As per your instructions, I downloaded the precompiled version on to CentOS machine
http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

  1. Should I be using only UBUNTU machine for this (Will CentOS OK for c++small Programs)
  2. Which C++ compiler should I be using here: there are many of them *uclibc++, C++filt etc..
  3. What path I need to set for the compiler to load libraries.
    Please provide me the details.

Is this does not work, then I wll down load the source from GitHub and compile. On that front How do I download the source zip on Linux using CURL command

As always, I appreciate your help.

Warm Regards,

BREL

I'm not familiar with centos except that is a popular "developer's distro". It shouldn't too hard to get it working. Googling centos openwrt should produce hints.
The link above about cross compiling on openwrt gives lots of details on how to get things set up as far as compiler paths and names.
I'm fairly sure I have compiled with that setup and successfully run programs on the Yun but that was many moons ago before I compiled the linino distro.
As Federico mentions, be VERY careful if you get to the point of flashing images and such. I have played mainly on routers with openwrt and gotten accustomed to flashing them, but I still put on my asbestos panties when I do. It is very easy to turn a neat toy into an expensive paperweight
Just uploading a program or package is much safer. Unless it does some strange stuff a reboot should take care of any issues.

Dear NoblePepper,

Thanks for the reply.

Can you please tell me which C++ compiler you used to compile the program for yun. That will help a lot.

I have attached the screen shot for your reference,

Thanks & regards,

BREL

You want mips-openwrt-linux-g++. Just knowing the proper compiler name won't get you very far...

I downloaded the SDK and followed the Eclipse instructions posted earlier to the point of making HelloOpenWrt. Watching the console output in Eclipse shows me that this method will compile a C++ program and run it on the Yun. You would really learn a lot by figuring it out yourself. :wink:

Unpack the SDK in your home directory then

cd ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/
mkdir test
cd test

Create a file "src/HelloOpenWrt.cpp" with these contents using whatever editor you like:

#include <iostream>
using namespace std;
int main() {
int i = 0;
for (i=0; i<10; i++)
{
cout << "Hello OpenWrt" << endl;
}
return 0;
}

Add the tools location to your path:

 PATH=$PATH:~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin

Now you can compile and link:

 mips-openwrt-linux-g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/HelloOpenWrt.d" -MT"src/HelloOpenWrt.d" -o "src/HelloOpenWrt.o" "src/HelloOpenWrt.cpp"
mips-openwrt-linux-g++  -o "HelloOpenWrt"  ./src/HelloOpenWrt.o

Copy the program and the C++ standard library to the Yun:

scp HelloOpenWrt root@arduino.local:/root
scp ../staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/lib/libstdc++.so.6 root@arduino.local://usr/lib

ssh into the Yun and run the program:

ssh root@arduino.local
./HelloOpenWrt

As I wrote earlier, this works for simple programs that don't rely on anything tied to tightly to the Yun, standard library calls seem to work fine, anything tied to the kernel version will definitely need the linino distro setup.

NoblePepper,

Super Super Super

I could able to compile small C++ program successfully and run in my arduino.

Tons of thanks for you.

I knew you can help me for this.

Hats off to you

Regards

BREL