★ wanayoo — archive 1999 http://codeforge.com/tutorials/CTutorial.htmlNouvelle recherche | Portail wanayoo
C-Forge home Simple C Tutorial


Creating a "C" Project

In order to help you become acquainted with C-Forge we will lead you through a simple project; let's call it "First". When "First" is completed, it will print the line "Hello, World!".
  1. Click the New Project toolbar button found in the C-Forge main window.

    This opens the New Project dialog box. This dialog already contains the Project Root /home/user.

  2. Type in the name "First" and click the OK button. A new directory called /home/user/First-prj has been created containing the the project file First.mak. In addition the New Target dialog is opened.

  3. Choose the first target of the project. The target name and directory layout defaults to the current project name. For this lesson leave them alone. We want to create a "C" program, so we choose the "Executable" target type from the list and click the OK button (C-Forge also supports a large variety of other languages). Since we decided to keep the default directory structure C-Forge has created a directory called /home/user/First/First-dir with sub directories Sources, Objects and Includes.

    Your Project Desktop window looks like this:

  4. Choose the New Source button, found in the Project Desktop toolbar to create a source file for the target. This will open the Add Source to Project Dialog.

  5. Now we will create a new file using the New File button to open the New File Dialog.

  6. Type the file name "First.c" and click the OK button. We have previously selected the default directory layout so the new file is created in the /home/user/First/First-dir/Sources directory. If we had checked the Revision Control check-box when we created this file C-Forge would have crated an RCS directory with an initial depository of 'First.c,v'. The dependency tree now looks like this:


  7. We are now ready to modify "First.c". Using the middle mouse button drag "First.c" from the Dependency Tree to the Working Area or Edit drop site.


    If the Revision Control option had been selected the Check-Out operation is automatically performed.

    • The default version is extracted from the RCS depository and locked.
    • That version is located and the file is copied to the users working diretory.
    • The file is placed in the editor window.
    • If the Insert RCS header option was checked, in the New File Dialog during the file creation step, RCS information is added to the top of the file.
    If the Revision Control option was not selected the file is copied to the the user working directory and placed in the editor.

  8. Type the following "C" code into the opened Editor Window:
    int main (void)
    {
          printf ("Hello, World!\n");
          return 1;
    }
    

  9. Select the Actions -> Make Target option found in the Editor Window menu. This will have the following affect:
    • The project log window is opened,
    • "First.c" file is compiled to "First.o", and "First.o" file is linked to "First" executable file:

  10. If no errors have been found, select the Actions -> Run Target from Editor menu to execute target "First".

Adding a Library to Project "First"

Now that we have a simple project ("First") up and running we are ready to add a library. For the purposes of this exercise we will name the library "libFirst".
  1. From the Project Desktop press the Add Target button located on the toolbar. This will open the New Target dialog.

  2. Type in the library name "libFirst.a" in the target field and choose "Library" item from the Type list. Once that is done click the OK button to continue. The Dependency Tree look like this:

  3. It is now time to add a source module to our library. To do this click the Add Source button found in the toolbar. This will open the Add Source to Project dialog.

  4. Now we will crate a new file using the New File button to open the New File dialog.

  5. Type the file name "libFirst.c". The Dependency Tree now looks like this:

  6. Using the middle mouse button drag "libFirst.c" from the Dependency Tree to the Working Area or Edit drop site.

  7. Next create the text for "libFirst.c" as we did it above.
    void libFirst (char *string)
    {
           printf (string);
    }
    

  8. Select the Actions -> Make Target option found in the Editor Window. This will have the following affect:
    • "libFirst.c" file will be compiled to "libFirst.o" file,
    • "libFirst.a" is created containing "libFirst.o".

  9. Now that we have built the library "First.c" needs to be modified to include a call to "libFirst". Drag "First.c" from the Dependency Tree to the Working Area or Edit drop site and make the following changes:

    extern void libFirst (char *);
    int main (void)
    {
          libFirst ("Hello, World!\n");
          return 1;
    }
    

  10. All that remains is to link the library to our target. To do this, Control-Shift-Drag'n'Drop (link) the target "libFirst.a" onto target "First".


    And the full dependency tree looks like this:

  11. Now, Drag'n'Drop target "First" to Make drop site to compile and link it.

  12. Finaly double click on "First" to run it.

 

 


Copyright © Code Forge, Inc. 1997-99. All rights reserved.


Last modified: Thursday, 30-Sep-1999 02:12:45 MDT