Documentation:NGW/CPPHelloWorld
From AVRFreaks Wiki
Contents |
[edit] Hello World
This HowTo will show you how to set up a AVR32 Studio project in Windows(tm), add the code, tell the linker where to find the needed library, compile the binary and run the executable via a terminal.
[edit] Setting up the project
Start AVR32 Studio and select: "File > New > Project..."
Then in the menu select: "AVR32 > Managed Make AVR32 C++ Project"
In the window that pops up, type the name of your project (I'll use "foo" as my example) and select AVR32 Linux Executable from the drop down list in "Project type" Leave the other options as they are and select finish.
Now you will have a new project ("foo" in my case) in the Project window on the left. Right Hand click on it and select "New > File > Other...". You'll then need to select "source file" from underneath the "C++" branch of the tree of file types.
In the window that pops up type a name for you file and end it with .cpp (in my case I have chosen "foo.cpp"). Then click finish on that screen.
You should now have a new entry under your project name in the left hand window.
Select the file you just created (in my case "foo.cpp").
Now the large centre window can have text added and be edited and saved to your file.
[edit] Adding the code
In the centre window add the following code:
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Hello World!\n");
return 0;
}
Save your file by clicking the save icon on the toolbar or selecting save from the File menu
[edit] Linker Settings
This program is C++ as opposed to C. It includes "stdio.h". This header links to a large library of standard input/output routines. We have two options when it comes to libraries. We can link to them dynamically at runtime or we can compile them into our executable at compile time.
The default NGW100 does not readily have enough room for the large dynamic library (libstdc++.so is about 4.5 megs at time of writing). A better solution therefore is to statically link the library. One advantage beyond the fact the executable can reside on the SD Card is that the linker will only link the needed pieces of the static library, leaving the remainder. This selection is not easily possible with dynamic libraries.
To that end, we need to tell the linker where to find the static library so it can link it in. The static library is called "libstdc++.a" and the dynamic, "libstdc++.so.6"
Select your project in the Project View window on the left.
Then select Project from the toolbar and click on Properties
In the window that appears, select "C/C++ Build" on the left panel.
In the long list that appear roughly in the centre, under the tab "Tool Settings", select "AVR32/GNU C++ Linker" and the under it, "Libraries".
Now you should have two panels appear on the right hand side of that window ("Libraries (-l)" and "Library search path (-L)".
Add an entry to the top (Libraries (-l)) panel of "stdc++". This is short for libstdc++.a but don't worry, the linker will know and add in the rest.
In the bottom window you will need to add a path the where "libstdc++.a" is found on your hard drive. You will find it under Cygwin.
On my machine it is "C:\Cygwin\usr\local\avr32-linux\lib\". If you want to, you can copy it to a more convenient location and direct the linker there.
(NOTE: If you do not point the project settings to a valid copy of the libstdc++.a file in the "Library search path (-L), AVR32Studio will quietly compile the project using dynamic libraries - this will manifest itself when you try to run the program and you get at unexpected "can't load library 'libstdc++.so.6'" error. Also the executable dynamically linked is on about 20Kb whereas statically linked is 220Kb)
I've added red dots to the image for areas you need to note.
Now the only thing left to do, is enable static linking. This can be done by navigating to "Miscellaneous" in the "AVR32/GNU C++ Linker" menu and adding "-static" to the "Other Options" box.
Now you should be able to compile the project without errors. By default automatic building is set on, if not go to "Projects>Build Project"
Hopefully there will be no errors from compilation. If this is the case, you will find an ".elf" file in the Debug folder of your project. This is the executable that will run on your NGW100.
[edit] Running the Program
If you are new to Linux this will appear unusual and tricky, but remember when you started with Mac/Windows that was also tricky and unusual at first, you soon get the hang of it.
I'm going to assume that you have an SD Card in your NGW100 and that you know how to either FTP/SMB/Telnet/SSH/USB the file across to it. So however you do it, place the file on the SD Card.
Now in a terminal, change directory to the SD Card ("cd /media/mmcblk0p1")
Now list the directory ("ls")
If your file does not appear in green then it is not set to be executable. If it does then do not worry about this step. Not being executable could be due to the method you used to transfer the file. To fix this we will use chmod. Assuming your file is called foo.elf, enter the command "chmod +x foo.elf". Of course change foo to you file name. +x adds the executable bit to the permissions, meaning you are explicitly telling it that you allow it to execute. (Please note that you can substitute +x for 777, which gives owner, group and other full permissions to read, write and execute the application. This is extremely insecure and should never be done).
(If your terminal does not support colour and when you try to execute the application you get "sh: ./foo.elf: Permission denied" that mean the application is not executable)
Now in the terminal, again assuming the file to be foo.elf, type "./foo.elf". The Hello World program should run and immediately below where you typed your command should appear "Hello World!"
(Side note - the reason we need to type "./foo.elf" to execute a command is because Linux only looks for applications that are on the executable path. If you've used DOS you'd expect to be able to execute any program in the current directory by just typing its name. In Linux, the current directory is not automatically checked for the application, that is why we need to add the "./" before the name of the program. This tell is Linux to check in the current directory, which is represented by "./")
If you've got here fine, well done and congratulations.
Below is an example of the terminal screen you might see. (Note, foo.elf was executable, as shown by the fact it appeared in green after executing the command "ls -l" but I thought I'd show the chmod command regardless)


