Building Applications

In the Sourcery G++ IDE, every program is a project. The project contains all of the source files required to build the program. So, the first step is to create a project.

There are two kinds of projects: “Managed Make” and “Standard Make” projects. In general, if you intend to do all of your development from within the IDE, you should use a Managed Make project. In this mode, the IDE automatically handles building your project for you. However, if you are working with code that has previously been built with make, you may wish to use a Standard Make project instead. The following several sections explain how to create and work with a Managed Make project. If you wish to use a Standard Make project instead, skip ahead to section the section called “Using Standard Make Mode”.

Setting Up

Create a new project by selecting FileNewProject. Expand the C label and select Managed Make C Project. (If you want to build a C++ application, expand the C++ label instead.) Click the Next button.

Figure 5.1. Creating a Project

Creating a Project
Expand the C folder and select the Managed Make C Project to create a new program.

Give the project the name factorial and click the Next button. From the Project Type menu select Executable (Sourcery G++ for ARM SymbianOS) and click Finish. If you are asked whether or not to open a new perspective, click the Yes button.

On ELF and EABI targets, you must choose a target board before you can link your application. On all targets, you may wish to choose a CPU other than the default so that Sourcery G++ can optimize for your processor. To set these options right-click on the factorial project, and select Properties. From the list on the left select C/C++ Build. From the Configuration Settings panel select the Tool Settings tab. Select the Target menu from the list and choose your target-specific options. If you have no target board use the Simulator board option, when available; otherwise choose the first board on the list.

At this point, the project exists, but there is no associated source code. So, the next step is to create the main program. Right-click on the factorial project, and select NewSource File. Give the new file the name main.c and click the Finish button.

Figure 5.2. Adding a Source File

Adding a Source File
Right-click on the project name to add a new source file.

Writing Source Code

Whenever you create or save a file, the Sourcery G++ IDE attempts to rebuild the program. Because the program is empty at this point, the compilation does not succeed, and you may notice some messages in the Console tab indicating errors. Those errors will go away when the program is completed.

The Sourcery G++ IDE now displays an editing window for you to use to create the program. Type (or cut-and-paste) the following program into the editor:

Example 5.1. Factorial Application

#include <stdio.h>

int factorial(int n) {
  if (n == 0)
    return 1;
  return n * factorial (n - 1);
}

int main () {
  int i;
  int n;
  for (i = 0; i < 10; ++i) {
    n = factorial (i);
    printf ("factorial(%d) = %d\n", i, n);
  }
  return 0;
}

When you are done, save the file with FileSave (Ctrl-S).

When you save the file, the Sourcery G++ IDE rebuilds the project. The output of the commands run by the IDE is displayed in the Console tab. You should see the following output at the bottom of the console:

Build complete for project factorial

Using Cross-Reference Information

Whenever it rebuilds your project, the Sourcery G++ IDE also computes cross-reference information. You can see some of this information in the Outline pane. In particular, each of the two functions in the program (factorial and main) are shown in the Outline pane. When you click on name of a function or variable in the Outline pane, the IDE repositions the cursor to show you that entity.

Figure 5.3. Using the Outline

Using the Outline
Click a function name in the Outline to jump to it in the editor.

You can also use the cross-reference information to jump from the place where a function is called to the definition of the function. For example, find the line in main which calls factorial and place the cursor over the name factorial. Then, right-click and select Open Declaration (F3) to jump to the point at which factorial is declared. The cross-reference functionality works even if the function call is in a different file from the declaration of the function.

Dealing with Errors

If you pasted the sample application into the IDE, the program probably compiled correctly the first time. But, of course, that rarely happens when writing a large program from scratch. To see how the Sourcery G++ IDE deals with errors, you can intentionally introduce an error.

Change the declaration of n in main to declare m, instead of n, and save the file. This change makes the program invalid because there are references to n in the function, but no declaration. In addition, the new variable m is not serving any useful purpose (since there are no references to it). Sourcery G++ informs you of both issues by flagging the problematic lines of source code.

The IDE places a circular red symbol next to lines that cause errors and a triangular yellow symbol on lines that cause warnings. There are several ways to get more detailed information about the problems. One way is to click on the Problems pane at the bottom of the IDE. This pane shows the error and/or warning messages issued by the compiler. Also, when you place the cursor over the error indicators, the IDE displays the error message.

Figure 5.4. Viewing Errors

Viewing Errors
Place the cursor over a warning or error indicator to see the cause of the problem.

Before proceeding, you must correct the error by changing m back to n.

Using Standard Make Mode

This section explains how to use the advanced Standard Make mode, instead of the simpler Managed Make mode described above. If you are just getting started with Sourcery G++, you should skip this section and proceed directly to the section called “Debugging Applications”.

Caution

Using Standard Make Mode requires that you manually maintain information about how your program is built. If you use this mode, you need to be familiar with the make utility.

If you want to import an existing project for use with the Sourcery G++ IDE, and that project uses make, or some similar command-line tool to manage the build process, you should use a Standard Make project, instead of a Managed Make project. In Standard Make mode, the IDE invokes make (or an alternative program that you specify) to build your program. If you add new files to your project, you have to manually update the Makefile for your project.

To set up the Standard Make mode to work with Sourcery G++, you have to make a few changes to the default project settings. When you create the project, the IDE displays a window that permits you to define the project settings.

Select the Discovery Options tab and set the Compiler invocation command to arm-none-symbianelf-gcc instead of the default gcc. That change tells the IDE to use the Sourcery G++ compilers when scanning your program code to determine cross-reference information. You may also have to adjust your Makefile to use Sourcery G++. For example, you might need to set the CC variable in your Makefile to arm-none-symbianelf-gcc.