Sep 6

Since sshfs and NFS are both not working I’m stuck with copying any executable program to the FB using the scp command. This not very convenient since the executable is expected to change many times during development.

In order to compile and link C-programs normally a MAKE file is used, this is also the case when using Anjuta. A make file is in principle nothing else than a batch file containing several commands that are executed depending the current state of any source file. They look very complex, but when the programs remain simple using only a couple of privatly controlled source files than the complexity reduces very quickly. It might be an option to create my own make file and include all required actions that need to take place after compiling.

In that case the work flow would be:

  • Start an ssh terminal ones and leave it running during the development session
  • Create the code, execute the make file that compiles, links and copies the result to the FB
  • In the ssh a new file is available that can be executed to see the results
  • If not OK, switch back to the editor, change the code and run make again

This can only work if all commands that must be executed do not requeire any user input during the execution. The scp command however does prompt for a password, so this must be fixed first.

A typical makefile contains lines of the following types:

  • Comments, any line beginning with a “#” sign, or any line that contains only white-space

For example:

# This is a makefile for the Hello_World application

  • Variable Definitions, these lines define values for variables

For example:

CFLAGS = -mlinux -o
SRCS = main.c file1.c file2.c
CC = gcc-cris
TARGET = Hello_World

  • Dependency Rules, these lines define under what conditions a given file (or a type of file) needs to be re-compiled, and how to compile it

For example:

main.o: main.c
gcc-cris -mlinux -o Hello_World main.c

This rule means that main.o has to be recompiled whenever main.c is modified. The rule continues to tell us that in order to compile main.o, the command gcc-cris -mlinux -o Hello_World main.c needs to be executed. Note that each line in the commands list must begin with a TAB character. Make is quite picky about the makefile’s syntax.

The definited variables can be used in any statement using $(variable name), the previous example of the main.o dependancy can be rewritten as follows:

main.o: main.c
$(CC) $(CFLAGS) $(TARGET) main.c

The benefit of this is clear, when multiple source files are use a change in the compiler flags only needs to be made in one place. Also it is very easy to change the compiler into gcc so that the output is now compiled for the development PC.

As a result, the basic makefile for the HelloWorld application is:

COMPILER = gcc-cris
CFLAGS = -mlinux -o
SOURCES = main.c
TARGET = Hello_World

# top-level rule to create the program, executed by default if no parameters are provided
all: compile

# compiling the source file.
compile: $(SOURCES)
<TAB>$(COMPILER) $(CFLAGS) $(TARGET) $(SOURCES)

Save the file in the same folder as the source files using the filename Makefile. In a terminal window navigate to the folder in which the makefile was stored and enter the command make. Since no parameter is given, make executes the first dependency it finds; in this case all. Since this depends compile is searches for the compile dependency. Compile depends on the variable SOURCES (main.c) and the associated command is executed.

Next the copy command is included:

COMPILER = gcc-cris
CFLAGS = -mlinux -o
SOURCES = main.c MyUnit.c
TARGET = Hello_World
DESTINATION = root@FOXBoard:/mnt/flash/bin/HelloWorld

# top-level rule to create the program, executed by default if no params are provided
all: compile

# compiling the source file.

compile: $(SOURCES)

<TAB>$(COMPILER) $(CFLAGS) $(TARGET) $(SOURCES)
<TAB>scp $(TARGET) $(DESTINATION)

When executed, the application is now compiled and copied to the FB and the really cool part is that if during execution of the commands an error is detected all next commands are ignored! This might happen if (by accident of course) an error is made in the source code. The compiler than exits with a non-zero exit value, this prevents make from executing all other commands.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.