Difference between revisions of "HelloWorld"

From ArmadeusWiki
Jump to: navigation, search
(Links)
(Putting it all together in a Makefile)
 
(31 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
 
On this page you will learn how to create your first C application for your Armadeus board
 
On this page you will learn how to create your first C application for your Armadeus board
  
 
==Source code==
 
==Source code==
First take your favorite editor/IDE and create the following program:
+
*First take your favorite editor/IDE and create the following program:
  
 
<source lang="c">
 
<source lang="c">
Line 10: Line 11:
 
int main(int argc, char *argv[])
 
int main(int argc, char *argv[])
 
{
 
{
     printf( "APF9328 says: Hello World ! ;-)\n" );
+
     printf( "APF says: Hello World ! ;-)\n" );
 
     exit(0);
 
     exit(0);
 
}
 
}
 
</source>
 
</source>
  
Save it as hello.c
+
*Save it as ''hello.c'' in YOUR_ARMADEUS_PROJECT_PATH/target/demos/ directory.
  
 
==Compilation==
 
==Compilation==
The C cross compiler is installed in ''armadeus/buildroot/build_arm/staging_dir/bin/'' and is named ''arm-linux-gcc''
+
The C cross compiler is installed in ''buildroot/output/host/usr/bin/'' and is named ''arm-linux-gcc''. You can access it with the ''$ARMADEUS_TOOLCHAIN_PATH'' environment variable:
 +
<pre class="host">
 +
$ cd YOUR_ARMADEUS_PROJECT_PATH/
 +
$ make shell_env
 +
$ source ./armadeus_env.sh
 +
$ 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/staging_dir/bin/'' to your ''PATH'' environment variable and then call ''arm-linux-gcc'' instead of ''gcc''
+
* either add ''$ARMADEUS_TOOLCHAIN_PATH'' to your ''PATH'' environment variable and then call ''arm-linux-gcc'' instead of ''gcc''
* or call directly ''armadeus/buildroot/build_arm/staging_dir/bin/arm-linux-gcc''
+
* or call directly ''$ARMADEUS_TOOLCHAIN_PATH/arm-linux-gcc''
  
So to compile your small program do (here hello.c was saved in armadeus/target/demos/ directory):
+
So to compile your small program (here ''hello.c'' was saved in ''armadeus/target/demos/'' directory) do:
[host demos]$ ../../buildroot/build_arm/staging_dir/bin/arm-linux-gcc -o hello hello.c
+
<pre class="host">
 +
$ cd YOUR_ARMADEUS_PROJECT_PATH/target/demos/
 +
$ $ARMADEUS_TOOLCHAIN_PATH/arm-linux-gcc -o hello hello.c
 +
</pre>
 +
or
 +
<pre class="host">
 +
$ export PATH=$PATH:$ARMADEUS_TOOLCHAIN_PATH
 +
$ cd YOUR_ARMADEUS_PROJECT_PATH/target/demos/
 +
$ arm-linux-gcc -o hello hello.c
 +
</pre>
  
 
==Running==
 
==Running==
 
Copy your ''hello'' executable on your board either through TFTP or NFS
 
Copy your ''hello'' executable on your board either through TFTP or NFS
 
===TFTP===
 
===TFTP===
Be sure to have TFTP server installed, [[Connection_with_U-Boot_on_Linux#TFTP_server| if not it's explained here]]<br>
+
* Be sure to have TFTP server installed, [[Connection_with_U-Boot_on_Linux#TFTP_server| if not it's explained here]]. Copy ''hello'' to your TFTP directory:
Copy ''hello'' to TFTP directory:
+
<pre class="host">
[host demos]$ cp hello /tftpboot/
+
$ cp hello /tftpboot/
Load your executable on the target (here my host IP is 192.168.0.2):
+
</pre>
 +
* Load your executable on the target (here my host IP is 192.168.0.2):
 +
<pre class="apf">
 
  # tftp -g -r hello -l /usr/bin/hello 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:
+
</pre>
 +
* Give it executable rights, if lost during TFTP transfer:
 +
<pre class="apf">
 
  # chmod a+x /usr/bin/hello
 
  # chmod a+x /usr/bin/hello
Launch it:
+
</pre>
 +
* Launch it:
 +
<pre class="apf">
 
  # /usr/bin/hello
 
  # /usr/bin/hello
  APF9328 says: Hello World ! ;-)
+
  APF says: Hello World ! ;-)
 
  #
 
  #
 +
</pre>
  
 
Now it's up to you ! ;-)
 
Now it's up to you ! ;-)
  
 
===NFS===
 
===NFS===
Be sure to have NFS server installed, [[Network_Configuration| if not it's explained here]]<br>
+
* Be sure to have NFS server installed, [[Network_Configuration| if not it's explained here]]. I now assume that your server has 192.168.0.2 as IP address and ''/local/exports/'' as NFS export directory.
I assume that your NFS drive is accessible from ''/mnt/host''
+
* Copy your executable:
Launch your prog:
+
<pre class="host">
  [target]# /mnt/host/hello
+
$ cp hello /local/exports/
 +
</pre>
 +
* Mount NFS on your target (I assume that your NFS drive is accessible from ''/mnt/host'' on the target):
 +
<pre class="apf">
 +
# mount -t nfs 192.168.0.2:/local/exports /mnt/host
 +
</pre>
 +
* Launch your prog:
 +
<pre class="apf">
 +
  # /mnt/host/hello
 +
</pre>
  
 
==Putting it all together in a Makefile==
 
==Putting it all together in a Makefile==
 
You can put your program compiling and copying in a ''Makefile'' to make things cleaner:
 
You can put your program compiling and copying in a ''Makefile'' to make things cleaner:
CC=arm-linux-gcc
+
<source lang="make">
CFLAGS=-W -Wall
+
CC=arm-linux-gcc
LDFLAGS=
+
CFLAGS=-W -Wall
EXEC=hello
+
LDFLAGS=
SRC= $(wildcard *.c)
+
 
OBJ= $(SRC:.c=.o)
+
EXEC=hello
 +
SRC=$(wildcard *.c)
 +
OBJ=$(SRC:.c=.o)
 
   
 
   
all: $(EXEC)
+
all: $(EXEC)
 
   
 
   
hello: $(OBJ)
+
$(EXEC): $(OBJ)
    $(CC) -o $@ $^ $(LDFLAGS)
+
    $(CC) -o $@ $^ $(LDFLAGS)
 
   
 
   
%.o: %.c
+
%.o: %.c
    $(CC) -o $@ -c $< $(CFLAGS)
+
    $(CC) -o $@ -c $< $(CFLAGS)
 
   
 
   
.PHONY: clean install
+
.PHONY: clean install
 
   
 
   
clean:
+
clean:
    rm -rf *.o
+
    rm -rf *.o
    rm -f $(EXEC)
+
    rm -f $(EXEC)
 
   
 
   
install: all
+
install: all
    cp -f $(EXEC) /tftpboot/
+
    cp -f $(EXEC) /tftpboot/
 +
</source>
  
'''!! 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'''
+
{{Warning| 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:
 
Then, just do:
[host demos]$ make clean install
+
<pre class="host">
 +
$ make clean install
 +
</pre>
  
 
==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]
+
* [http://www.advancedlinuxprogramming-fr.org/doku.php Livre en ligne: Programmation Linux Avancée]  
 
+
<br>[[Image:FrenchFlag.png]][[Fr:HelloWorld| Cette page en français]]
+
  
 
[[Category:Software]]
 
[[Category:Software]]
 
[[Category:Programming language]]
 
[[Category:Programming language]]

Latest revision as of 20:14, 22 September 2015

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 <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    printf( "APF says: Hello World ! ;-)\n" );
    exit(0);
}
  • Save it as hello.c in YOUR_ARMADEUS_PROJECT_PATH/target/demos/ directory.

Compilation

The C cross compiler is installed in buildroot/output/host/usr/bin/ and is named arm-linux-gcc. You can access it with the $ARMADEUS_TOOLCHAIN_PATH environment variable:

 $ cd YOUR_ARMADEUS_PROJECT_PATH/
 $ make shell_env
 $ source ./armadeus_env.sh
 $ 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-gcc instead of gcc
  • or call directly $ARMADEUS_TOOLCHAIN_PATH/arm-linux-gcc

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

$ cd YOUR_ARMADEUS_PROJECT_PATH/target/demos/
$ $ARMADEUS_TOOLCHAIN_PATH/arm-linux-gcc -o hello hello.c

or

$ export PATH=$PATH:$ARMADEUS_TOOLCHAIN_PATH
$ cd YOUR_ARMADEUS_PROJECT_PATH/target/demos/
$ arm-linux-gcc -o hello hello.c

Running

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

TFTP

 $ 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
 APF says: Hello World ! ;-)
 #

Now it's up to you ! ;-)

NFS

  • Be sure to have NFS server installed, if not it's explained here. I now assume that your server has 192.168.0.2 as IP address and /local/exports/ as NFS export directory.
  • Copy your executable:
 $ cp hello /local/exports/
  • Mount NFS on your target (I assume that your NFS drive is accessible from /mnt/host on the target):
 # mount -t nfs 192.168.0.2:/local/exports /mnt/host
  • Launch your prog:
 # /mnt/host/hello

Putting it all together in a Makefile

You can put your program compiling and copying in a Makefile to make things cleaner:

CC=arm-linux-gcc
CFLAGS=-W -Wall
LDFLAGS=

EXEC=hello
SRC=$(wildcard *.c)
OBJ=$(SRC:.c=.o)
 
all: $(EXEC)
 
$(EXEC): $(OBJ)
    $(CC) -o $@ $^ $(LDFLAGS)
 
%.o: %.c
    $(CC) -o $@ -c $< $(CFLAGS)
 
.PHONY: clean install
 
clean:
    rm -rf *.o
    rm -f $(EXEC)
 
install: all
    cp -f $(EXEC) /tftpboot/
Warning Warning: 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:

$ make clean install

Links