Difference between revisions of "HelloWorldCpp"

From ArmadeusWiki
Jump to: navigation, search
m (Makefile improving)
m
 
(11 intermediate revisions by 3 users not shown)
Line 4: Line 4:
 
First take your favorite editor/IDE and create the following program:
 
First take your favorite editor/IDE and create the following program:
  
#include <iostream>
+
<source lang="cpp">
+
#include <iostream>
using namespace std;
+
 
+
using namespace std;
class Hello
+
 
{
+
class Hello
    private:
+
{
        const char* message;
+
    private:
+
        const char* message;
    public:
+
 
        Hello( const char* amessage ) { message = amessage; };
+
    public:
        virtual ~Hello() { cout << "Bye, bye !" << endl; };
+
      Hello( const char* amessage ) { message = amessage; };
+
      virtual ~Hello() { cout << "Bye, bye !" << endl; };
        void speaks() { cout << message << endl; };
+
 
};
+
      void speaks() { cout << message << endl; };
+
};
int main()
+
{
+
    Hello hello( "APF9328 says Hello everybody !" );
+
    hello.speaks();
+
    return 0;
+
}
+
  
 +
int main()
 +
{
 +
    Hello hello( "APF9328 says Hello everybody !" );
 +
    hello.speaks();
 +
    return 0;
 +
}
 +
</source>
 
Save it as ''hello.cpp''
 
Save it as ''hello.cpp''
  
 
==Compilation==
 
==Compilation==
The C++ cross compiler is installed in ''armadeus/buildroot/build_arm_nofpu/staging_dir/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_arm_nofpu/staging_dir/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_arm_nofpu/staging_dir/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_arm_nofpu/staging_dir/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 90: Line 103:
  
 
Then, just do:
 
Then, just do:
  [host demos]$ make
+
  [host demos]$ make clean install
  
 
==Links==
 
==Links==
 
* [http://www.handhelds.org/minihowto/porting-software.html Things to know when porting x86 software to ARM]
 
* [http://www.handhelds.org/minihowto/porting-software.html Things to know when porting x86 software to ARM]
* [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]
 +
 
 +
[[Category:Software]]
 +
[[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