Книга: Embedded Linux development using Eclipse

4.3 Adding Source Code to the Project

There are basically two ways to add source code to a C project. You can, of course, create a new file in an Editor window, or you can import existing files into the project. Execute File?Import… to bring up the Import Select dialog. Expand the General category and select File System. Click Next, then click Browse, and navigate to Home/EclipseSamples/record_sort and click OK.

This brings up the Import dialog shown in Figure 4.2. Select all three files and click Finish. Those files now show up in the Project Explorer view. Note that there is no record_sort.c file. That’s because you’re going to type it in yourself to get some practice with the CDT editor.


Figure 4.2: Import dialog.

Click the down arrow next to the New icon at the left end of the tool bar and select Source File from the drop down menu. Name it “record_sort.c.” An Editor window opens up with a preliminary header comment. The contents of record_sort.c are given in Figure 4.3, but don’t start typing until you read the next section.

/*
 * author Doug Abbott
 *
 * Simple demonstration of building and running a project under
 * Eclipse.
 *
 * Usage:
 * record_sort <filename> [1 | 2]
 *
 * Sorts records in <filename> either by name (1) or ID (2).
 * Default is name. Outputs sorted file to stdout.
*/
#include <stdio.h>
#include <stdlib.h>
#include "record_sort.h"
int main (int argc, char **argv) {
 int size, sort = 1;
 record_t *records;
 if (read_file (argv[1], &size, &records)) {
  printf ("Couldn't open file %sn", argv[1]);
  exit(1);
 }
 if (argc > 2) sort = atoi (argv[2]);
 switch (sort) {
 case 1:
  sort_name (size, records);
  break;
 case 2:
  sort_ID (size, records);
  break;
 default:
  printf ("Invalid sort argumentn");
  return_records (size, records);
  exit (2);
 }
 write_sorted (size, records);
 return_records (size, records);
 return 0;
}

Figure 4.3: record_sort.c.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.061. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз