Difference between revisions of "Using EMAC OE SDK Example Projects"

From wiki.emacinc.com
Jump to: navigation, search
m
Line 1: Line 1:
{{todo|Make sure there is an OE 4 and OE 5 version; OE 5 will use CMake|Michael Gloff|project=oe 4,oe 5,mg}}
+
{{todo|<strike>Polish</strike>(11.26.13-22:36->MG+)|Michael Gloff|project=oe 4,mg,ky,Review}}
  
{{EMAC OE SDK Conventions}}
+
The EMAC OE SDK is distributed with a set of example projects intended to demonstrate how to use the EMAC OE toolchain and libraries. This guide demonstrates the process of compiling one of the example projects and running it on the target machine. It is assumed that the procedure in the [[ Configuring_EMAC_OE_4.0_SDK | EMAC OE SDK Configuration Guide]] has been followed prior to reading this page.
  
The EMAC OE SDK is distributed with a set of example projects intended to demonstrate how to use the EMAC OE toolchain and libraries. This guide demonstrates the process of compiling one of the example projects and running it on the target machine. NOTE: It is assumed that the procedure in the EMAC OE SDK Configuration Guide has been followed prior to reading this page.  
+
This guide consists of a simple hello.c source file and a Makefile build file located within Listing 2 and Listing 3, respectively.  
  
This guide consists of two example files, a C file and a Makefile. They are located within Listing 2 and Listing 3, respectively.
+
<br clear=all>
  
 
==Tools==
 
==Tools==
Line 11: Line 11:
 
* GNU <code>make</code>. Manages project dependencies.
 
* GNU <code>make</code>. Manages project dependencies.
 
* EMAC OE SDK. [See the EMAC OE SDK Install Page].
 
* EMAC OE SDK. [See the EMAC OE SDK Install Page].
* <code>wput</code>, Provides FTP capability to the build process.
+
* <code>wput</code>, Provides file transfer (FTP) capability.
 +
 
  
 
==EMAC SDK Example: Compile the "hello" Project==
 
==EMAC SDK Example: Compile the "hello" Project==
  
This procedure provides an overview of how to compile and run C applications for EMAC products. It assumes familiarity with the C programming language and is intended to be used by experienced programmers who are looking to learn the EMAC SDK. The “hello” example project source can be found in the projects/ subdirectory of the EMAC OE SDK root directory. The full path to the source is as follows:  
+
This procedure provides an overview of how to compile and run C applications for EMAC products. It assumes familiarity with the C programming language and is intended to be used by experienced programmers. The “hello” example project source can be found in the projects/ subdirectory of the EMAC OE SDK root directory. The full path to the source is as follows:  
  
 
<syntaxhighlight lang="console">
 
<syntaxhighlight lang="console">
Line 21: Line 22:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Cross-compile the program using GNU Make.
+
 
 +
Cross-compile the program using GNU Make. From the terminal prompt type:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 28: Line 30:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<code>all</code> is a make target. This command is the equivalent to the commonly seen, Build All command found in most IDE's. The build system will only compile files which have been modified since the last build.
+
The file <code>Makefile</code> specifies what Make targets are available and what actions should be performed for each target. <code>all</code> is a make target that directs Make to compile all source files for the project. This command is the equivalent to the Build All command found in most IDE's. The build system will only compile files which have been modified since the last build.
 +
 
  
Alternatively, you can simply run:
+
The Makefile based build system fully supports incremental builds. If a full rebuild is required, issue the following two commands.
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 +
developer@ldc:~$ make clean
 
developer@ldc:~$ make
 
developer@ldc:~$ make
 
</syntaxhighlight>
 
</syntaxhighlight>
  
When <code>make</code> is run by itself, it chooses its default make target. In this example, <code>all</code> is the default make target. In any normal project, the default make target will be the one which builds the project. This allows a programmer to simply run <code>make</code> most of the time they are doing development work.
 
  
The Makefile based build system fully supports incremental builds. Should you desire a full rebuild, two steps will need to be followed instead of one:
+
The <code>make clean</code> command will delete any object files (with the <code>.o</code> extension), debugger information files (with the <code>.gdb</code> extension), executable binaries (generally, no extension) and any other files which are generated during the build process. Once this cleaning step is complete, the build system will attempt to rebuild everything.
 
 
<syntaxhighlight lang="bash">
 
developer@ldc:~$ make clean
 
developer@ldc:~$ make
 
</syntaxhighlight>
 
  
The <code>make clean</code> command will delete any object files (these have the <code>.o</code> extension in the gcc on Linux build system), debugger information files (with the <code>.gdb</code> extension), executable binaries (generally, no extension) and any other files which are generated during the build process. Once this cleaning step is complete, the build system will attempt to rebuild everything.
+
For a more in-depth explanation on the gcc build process, see the [http://www.gnu.org/software/make/manual/make.html GNU "make" Manual].
  
For a more in-depth explanation on how gcc approaches the incremental build process, see the [http://www.gnu.org/software/make/manual/make.html GNU "make" Manual].
 
  
 
==EMAC SDK Example: Uploading and Running the "hello" Project"==
 
==EMAC SDK Example: Uploading and Running the "hello" Project"==
Line 56: Line 53:
 
developer@ldc:~$ make upload
 
developer@ldc:~$ make upload
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
<code>upload</code> is another make target. This command will send your freshly compiled binary to the target machine.
 
<code>upload</code> is another make target. This command will send your freshly compiled binary to the target machine.
  
This make target uses the development system's <code>wput</code> program to send the binary to the target machine through FTP. This is accomplished using variables stored in the <code>global.properties</code> file, which is included in the Makefile using the <code>include</code> keyword. The <code>global.properties</code> file also contains variables which <code>make</code> passes to the compiler to ensure that the executable produced is compatible with the target CPU architecture.  
+
This <code>Make</code> target uses the development system's <code>wput</code> program to send the binary to the target machine through FTP. This is accomplished using the TARGET_IP, LOGIN, and PASSWORD variables stored in the <code>global.properties</code> file. The <code>global.properties</code> file also contains variables which <code>Make</code> passes to the compiler to ensure that the executable produced is compatible with the target CPU architecture.  
  
 
===Running the Program on the Target Machine===
 
===Running the Program on the Target Machine===
  
Connect remotely to the target machine. Run the program as shown in Listing 1 using the remote connection.
+
Connect remotely to the target machine using SSH and run the program as shown below.
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 +
developer@ldc:~$ ssh root@TARGET_IP
 +
...
 
root@emac-oe:~# chmod u+x /tmp/hello
 
root@emac-oe:~# chmod u+x /tmp/hello
 
root@emac-oe:~# /tmp/hello
 
root@emac-oe:~# /tmp/hello
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
'''Listing 1. Running the Remote Application'''
 
'''Listing 1. Running the Remote Application'''
  
{{mbox | type = notice | text = <code>chmod u+x</code> sets the root user's permissions to allow execution of the binary. Note that this assumes that you are logged in as the system root user.}}
+
* <code>chmod u+x</code> sets the root user's permissions to allow execution of the binary.
 +
* <code>/tmp/hello</code> runs the program without any arguments.
  
{{mbox | type = notice | text = <code>/tmp/hello</code> runs the program without any arguments.}}
 
  
{{mbox | type = notice | text = '''NOTE:''' If you wish to run a program located in your current directory, you must run it as <code>./hello</code>. Without the <code>./</code> prefix, the program will not be run by the shell. The shell will only look for a program in the default search <code>PATH</code>, unless the program's name is prefixed with a specific path. The <code>./</code> prefix is a relative path. The <code>.</code> represents the current directory, just as <code>..</code> represents the previous directory. The <code>/</code> following the <code>.</code> indicates to the shell to look inside the current directory. In POSIX systems, such as Linux, filenames can have a <code>.</code> as their first character, which is why the <code>/</code> is needed.  
+
{{mbox | type = notice | text = '''NOTE:''' In order to run a program located in the current working directory, it must be run as <code>./hello</code>. The shell will only look for a program in the default search <code>PATH</code>, unless the program's name is prefixed with a complete path to it's location.}}
  
Incidentally, a <code>.</code> as the first character of the filename indicates that the file is hidden. Use the <code>ls -a</code> command to see such files.}}
 
  
If the file compiles without trouble but has runtime bugs, you can remotely debug the application using gdbserver. Read the [[EMAC OE SDK Remote Debugging Guide]] to learn more.
+
If the file compiles without trouble but has runtime bugs, you can remotely debug the application using gdbserver. Read the [[ Remote_Debugging_EMAC_OE_SDK_Projects_with_gdbserver | EMAC OE SDK Remote Debugging Guide]] to learn more.
  
 
===Example C File===
 
===Example C File===
Line 144: Line 144:
 
-include $(DEPS)
 
-include $(DEPS)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
'''Listing 3. Example EMAC OE SDK Makefile'''
 
'''Listing 3. Example EMAC OE SDK Makefile'''
  
{{mbox | type = notice | text = '''NOTE:''' Using the documentation for GNU Make will be required to gain a solid understanding of the build system. Here is a brief description of this Makefile to help you get started.
+
'''NOTE:''' Using the documentation for GNU <code>make</code> will be required to gain a solid understanding of the build system. Following is a brief description of this Makefile.
* The <code>include</code> command tells <code>make</code> to pull in the file specified in a way similar to the C/C++ <code>#include</code> preprocessor directive.
+
* The <code>include</code> command tells <code>make</code> to also look at the file specified for variable definitions.
* The <code>TARGET=</code> directive tells <code>make</code> the name and location it should use for any executable binaries or shared object binaries (shared objects are similar to Windows <code>.dlls</code>) it produces.
+
* The <code>TARGET=</code> directive tells <code>make</code> the name and location it should use for any executable binaries or shared object binaries it produces.
 
* The <code>CFILES=</code> directive tells <code>make</code> the names of the C source files it needs to build.
 
* The <code>CFILES=</code> directive tells <code>make</code> the names of the C source files it needs to build.
 
* The <code>OBJS=</code> and <code>DEPS=</code> directives tell <code>make</code> how to produce file names for the object and dependency files it generates. This file specifies a method for these which will work for most projects, and is unlikely to need modification.
 
* The <code>OBJS=</code> and <code>DEPS=</code> directives tell <code>make</code> how to produce file names for the object and dependency files it generates. This file specifies a method for these which will work for most projects, and is unlikely to need modification.
 
* <code>all:</code> specifies the make target named “all.” <code>clean:</code> and <code>upload:</code>, similarly, specify the make targets for targets of these names, respectively. New make targets can be given names this way. make will perform all the steps listed after the target name until it encounters another target name.
 
* <code>all:</code> specifies the make target named “all.” <code>clean:</code> and <code>upload:</code>, similarly, specify the make targets for targets of these names, respectively. New make targets can be given names this way. make will perform all the steps listed after the target name until it encounters another target name.
* The <code>CC</code> variable refers to the <code>gcc</code> command, because <code>CC</code> is so defined in the <code>global.properties</code> file included by this Makefile. The <code>$(CC)</code> syntax tells <code>make</code> to read the value of the variable, <code>CC</code>, and replace <code>$(CC)</code> with the value of <code>CC</code> prior to attempting to execute the command. This is similar to the way many scripting languages work, and hopefully will feel pretty natural to any developer working with Makefiles for the first time.}}
+
* The <code>CC</code> variable refers to the <code>gcc</code> command defined in the <code>global.properties</code> file. The <code>$(CC)</code> syntax tells <code>make</code> to read the value of the variable, <code>CC</code>, and replace <code>$(CC)</code> with the value of <code>CC</code> prior to attempting to execute the command.  
 +
 
 +
 
 +
{{mbox | type = notice | text = '''NOTE:''' This is a basic Makefile which can be extended as needed to support many projects. More complex projects may require the use of a ''build system builder'' tool to help generate scripts that can be used to build a project. Options include:
 +
* Learning the <code>make</code> system thoroughly.
 +
* Adopt a build system builder tool such as''CMake''. ''CMake'' is a simple yet powerful tool for dealing with large projects.
 +
* The Eclipse IDE contains a build system builder that may be able to handle the complex requirements of the project.}}
  
{{mbox | type = notice | text = '''NOTE:''' This is a simple, basic Makefile which can be extended as needed to support many projects. Should your build requirements become complex, more sophistication may be required. A ''build system builder'' may be the tool you need. If this becomes necessary, options you may wish to consider include:
 
* Buy a good book on <code>make</code> and learn the system thoroughly.
 
* Adopt a build system builder tool. ''CMake'' has proven to be popular for many large projects, and is simpler to use than ''autotools''. Buying a book on the build system builder tool chosen may prove wise.
 
* The build system builder in Eclipse may be able to handle the complexity of your requirements. This is probably the simplest option.
 
''NOTE: '''Build system builder''' refers to a tool which generates a set of scripts and/or Makefiles which are then used to build a project.''
 
}}
 
  
 
==Next Steps==
 
==Next Steps==
  
After compiling and running an example project, the next step is to create a new project. The [[EMAC OE SDK New Project Guide]] details this process from start to finish.
+
After compiling and running an example project, the next step is to create a new project from scratch. The [[Creating_a_New_EMAC_OE_SDK_Project | EMAC OE SDK New Project Guide]] details this process from start to finish.
  
 
[[Category:EMAC OE SDK]]
 
[[Category:EMAC OE SDK]]

Revision as of 23:38, 26 November 2013

TODO: {{#todo:Polish(11.26.13-22:36->MG+)|Michael Gloff|oe 4,mg,ky,Review}}

The EMAC OE SDK is distributed with a set of example projects intended to demonstrate how to use the EMAC OE toolchain and libraries. This guide demonstrates the process of compiling one of the example projects and running it on the target machine. It is assumed that the procedure in the EMAC OE SDK Configuration Guide has been followed prior to reading this page.

This guide consists of a simple hello.c source file and a Makefile build file located within Listing 2 and Listing 3, respectively.


Tools

  • GNU make. Manages project dependencies.
  • EMAC OE SDK. [See the EMAC OE SDK Install Page].
  • wput, Provides file transfer (FTP) capability.


EMAC SDK Example: Compile the "hello" Project

This procedure provides an overview of how to compile and run C applications for EMAC products. It assumes familiarity with the C programming language and is intended to be used by experienced programmers. The “hello” example project source can be found in the projects/ subdirectory of the EMAC OE SDK root directory. The full path to the source is as follows:

/path/to/sdk/EMAC-OE-arm-linux-gnueabi-SDK_XX.YY/projects/hello/


Cross-compile the program using GNU Make. From the terminal prompt type:

developer@ldc:~$ cd /path/to/sdk/EMAC-OE-arm-linux-gnueabi-SDK_XX.YY/projects/hello/
developer@ldc:~$ make all

The file Makefile specifies what Make targets are available and what actions should be performed for each target. all is a make target that directs Make to compile all source files for the project. This command is the equivalent to the Build All command found in most IDE's. The build system will only compile files which have been modified since the last build.


The Makefile based build system fully supports incremental builds. If a full rebuild is required, issue the following two commands.

developer@ldc:~$ make clean
developer@ldc:~$ make


The make clean command will delete any object files (with the .o extension), debugger information files (with the .gdb extension), executable binaries (generally, no extension) and any other files which are generated during the build process. Once this cleaning step is complete, the build system will attempt to rebuild everything.

For a more in-depth explanation on the gcc build process, see the GNU "make" Manual.


EMAC SDK Example: Uploading and Running the "hello" Project"

Uploading the Program to the Target Machine

developer@ldc:~$ make upload


upload is another make target. This command will send your freshly compiled binary to the target machine.

This Make target uses the development system's wput program to send the binary to the target machine through FTP. This is accomplished using the TARGET_IP, LOGIN, and PASSWORD variables stored in the global.properties file. The global.properties file also contains variables which Make passes to the compiler to ensure that the executable produced is compatible with the target CPU architecture.

Running the Program on the Target Machine

Connect remotely to the target machine using SSH and run the program as shown below.

developer@ldc:~$ ssh root@TARGET_IP
...
root@emac-oe:~# chmod u+x /tmp/hello
root@emac-oe:~# /tmp/hello

Listing 1. Running the Remote Application

  • chmod u+x sets the root user's permissions to allow execution of the binary.
  • /tmp/hello runs the program without any arguments.



If the file compiles without trouble but has runtime bugs, you can remotely debug the application using gdbserver. Read the EMAC OE SDK Remote Debugging Guide to learn more.

Example C File

This C file can be used by programmers as an example to ensure their build configuration for EMAC products is functioning correctly.

/**
 * @file hello.c
 *
 * Simple Hello World application for EMAC OE.
 *
 * @author EMAC, Inc. <support@emacinc.com>
 */
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    printf("Hello EMAC OE!\n");
    exit(EXIT_SUCCESS);
}

Listing 2. "Hello World" example project

Example Makefile

Listing 3 shows the default Makefile used for the hello example project. This is a necessary component of the EMAC SDK which directs GNU Make in resolving source code dependencies before calling the cross-compiler to create a binary for the target platform. It also provides a convenient upload target which utilizes the development system's wput command to send the compiled binary to the target system.

include ../global.properties
 
TARGET=hello
CFILES=hello.c
 
 
OBJS=$(CFILES:.c=.o)
DEPS=$(OBJS:.o=.d)
 
all: $(TARGET)
 
$(TARGET): $(OBJS) Makefile
  $(CC) $(VERBOSE) $(OBJS) $(OFLAGS) $(LIBFLAGS) $(SLIBS) -o $@
 
%.o: %.c
  $(CC) $(VERBOSE) $(CFLAGS) -o $@ -c $<
 
clean:
  $(RM) *.o *.gdb $(TARGET) $(DEPS)
 
upload: all
  $(WPUT) $(TARGET) ftp://$(LOGIN):$(PASSWORD)@$(TARGET_IP)/../../tmp/$(TARGET)
 
 
-include $(DEPS)

Listing 3. Example EMAC OE SDK Makefile

NOTE: Using the documentation for GNU make will be required to gain a solid understanding of the build system. Following is a brief description of this Makefile.

  • The include command tells make to also look at the file specified for variable definitions.
  • The TARGET= directive tells make the name and location it should use for any executable binaries or shared object binaries it produces.
  • The CFILES= directive tells make the names of the C source files it needs to build.
  • The OBJS= and DEPS= directives tell make how to produce file names for the object and dependency files it generates. This file specifies a method for these which will work for most projects, and is unlikely to need modification.
  • all: specifies the make target named “all.” clean: and upload:, similarly, specify the make targets for targets of these names, respectively. New make targets can be given names this way. make will perform all the steps listed after the target name until it encounters another target name.
  • The CC variable refers to the gcc command defined in the global.properties file. The $(CC) syntax tells make to read the value of the variable, CC, and replace $(CC) with the value of CC prior to attempting to execute the command.



Next Steps

After compiling and running an example project, the next step is to create a new project from scratch. The EMAC OE SDK New Project Guide details this process from start to finish.