Difference between revisions of "HelloWorldCpp"

From ArmadeusWiki
Jump to: navigation, search
m (Compilation)
m
 
(One intermediate revision by one other user not shown)
Line 31: Line 31:
  
 
==Compilation==
 
==Compilation==
The C++ cross compiler is installed in ''armadeus/buildroot/build_armv4t/staging_dir/usr/bin/'' and is named ''arm-linux-g++''
+
* First of all get the right environment variables:
 +
<pre class="host">
 +
$ make shell_env
 +
$ . armadeus_env.sh
 +
</pre>
 +
* The C++ cross compiler is installed in ''$ARMADEUS_TOOLCHAIN_PATH'' and is named ''arm-linux-g++''
 +
<pre class="host">
 +
$ echo $ARMADEUS_TOOLCHAIN_PATH
 +
</pre>
 
There are 2 possibilities to use it:
 
There are 2 possibilities to use it:
* either add ''armadeus/buildroot/build_armv4t/staging_dir/usr/bin/'' to your ''PATH'' environment variable and then call ''arm-linux-g++'' instead of ''g++''
+
* either add ''$ARMADEUS_TOOLCHAIN_PATH'' to your ''PATH'' environment variable and then call ''arm-linux-g++'' instead of ''g++'':
* or call directly ''armadeus/buildroot/build_armv4t/staging_dir/usr/bin/arm-linux-g++''
+
<pre class="host">
 
+
$ export PATH=$PATH:$ARMADEUS_TOOLCHAIN_PATH
So to compile your small program do (here hello.cpp was saved in armadeus/target/demos/ directory):
+
</pre>
  [host demos]$ ../../buildroot/build_armv4t/staging_dir/usr/bin/arm-linux-g++ -o hello hello.cpp
+
* or call directly ''$ARMADEUS_TOOLCHAIN_PATH/arm-linux-g++'':
 +
So to compile your small program do (here ''hello.cpp'' was saved in ''armadeus/target/demos/'' directory):
 +
<pre class="host">
 +
  [host]$ $ARMADEUS_TOOLCHAIN_PATH/arm-linux-g++ -o hello hello.cpp
 +
</pre>
  
 
==Running==
 
==Running==
Line 97: Line 109:
 
* [http://gl.developpez.com/tutoriel/outil/makefile/ Les Makefiles, comment ça marche ?]
 
* [http://gl.developpez.com/tutoriel/outil/makefile/ Les Makefiles, comment ça marche ?]
 
* [http://www.advancedlinuxprogramming-fr.org/doku.php| Livre en ligne Programmation Linux Avancée]
 
* [http://www.advancedlinuxprogramming-fr.org/doku.php| Livre en ligne Programmation Linux Avancée]
 
{{LanguageBar|HelloWorldCpp|HelloWorldCpp|HelloWorldCpp}}
 
  
 
[[Category:Software]]
 
[[Category:Software]]
 
[[Category:Programming language]]
 
[[Category:Programming language]]

Latest revision as of 19:57, 1 August 2010

On this page you will learn how to create your first C++ application for your Armadeus board

Source code

First take your favorite editor/IDE and create the following program:

#include <iostream>

using namespace std;

class Hello
{
    private:
        const char* message;

    public:
       Hello( const char* amessage ) { message = amessage; };
       virtual ~Hello() { cout << "Bye, bye !" << endl; };

       void speaks() { cout << message << endl; };
};

int main()
{
    Hello hello( "APF9328 says Hello everybody !" );
    hello.speaks();
    return 0;
}

Save it as hello.cpp

Compilation

  • First of all get the right environment variables:
 $ make shell_env
 $ . armadeus_env.sh
  • The C++ cross compiler is installed in $ARMADEUS_TOOLCHAIN_PATH and is named arm-linux-g++
 $ echo $ARMADEUS_TOOLCHAIN_PATH

There are 2 possibilities to use it:

  • either add $ARMADEUS_TOOLCHAIN_PATH to your PATH environment variable and then call arm-linux-g++ instead of g++:
 $ export PATH=$PATH:$ARMADEUS_TOOLCHAIN_PATH
  • or call directly $ARMADEUS_TOOLCHAIN_PATH/arm-linux-g++:

So to compile your small program do (here hello.cpp was saved in armadeus/target/demos/ directory):

 [host]$ $ARMADEUS_TOOLCHAIN_PATH/arm-linux-g++ -o hello hello.cpp

Running

Copy your hello executable on your board either through TFTP or NFS

TFTP

Be sure to have TFTP server installed, if not it's explained here
Copy hello to TFTP directory:

[host demos]$ cp hello /tftpboot/

Load your executable on the target (here my host IP is 192.168.0.2):

# tftp -g -r hello -l /usr/bin/hello 192.168.0.2

Give it executable rights, if lost during TFTP transfer:

# chmod a+x /usr/bin/hello

Launch it:

# /usr/bin/hello
APF9328 says: Hello World ! ;-)
#

Now it's up to you ! ;-)

NFS

Be sure to have NFS server installed, if not it's explained here
I assume that your NFS drive is accessible from /mnt/host Launch your prog:

[target]# /mnt/host/hello

Putting it all together in a Makefile

You can put your program compilation and installation in a Makefile to make things cleaner:

CXX=arm-linux-g++
CXXFLAGS=-W -Wall
LDFLAGS=
EXEC=hello
SRC= $(wildcard *.cpp)
OBJ= $(SRC:.c=.o)

all: $(EXEC)

hello: $(OBJ)
    $(CXX) -o $@ $^ $(LDFLAGS)

%.o: %.c
    $(CXX) -o $@ -c $< $(CXXFLAGS)

.PHONY: clean install

clean:
    rm -rf *.o
    rm -f $(EXEC)

install: all
    cp -f $(EXEC) /tftpboot/

!! If you do a cut & paste with previous commands, don't forget to check TABS for each target (Makefiles are using TABS and not SPACES) !! In that case make will complains about a missing separator line 11

Then, just do:

[host demos]$ make clean install

Links