Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc Intel XENIX 286 Programmers Guide (86) - Tenox.tc

09.06.2013 Views

XENIX Programming cc: C Compiler Using Object Files and Libraries The cc command can save useful functions as object files and use these object files to create programs at a later time. Because obj ect files contain the compiled or assembled instructions of your source file, they save you the time and trouble of recompiling the functions each time you need them. All object files created by cc have the file extension ".o". The cc command can also use functions found in XENIX system libraries, such as the standard C library or the screen processing library curses. To use these functions, you simply supply the name of the library containing them. However, cc accesses the standard C library automatically and no explicit naming is required. For convenience, you can create your own libraries with the ar and ranlib commands. These commands, described in Appendix B, "Programming Commands," copy your useful object files to a library file and prepare the file for use by the cc command. You can access the library like any other library in the system if you copy it to the /lib directory. You must name the library appropriately. The name must begin with a S, M, L, or H, identifying the model in which the library was compiled. Then, follow the capital letter with lib and the name you chose. Finally, supply the .a extension. For example, the small model curses library has the name Slibcurses.a. Creating Object Files You can create an object file from a given source file by using the -c (for "compile") option. This option directs cc to compile the source file without creating a final program. The option has the form -c filename ... where filename is the name of the source file. You may give more than one file name if you wish. Make sure each name is separated from the next by a space. To make object files for the source files add.c and mult.c, type cc -c add.c mult.c This command compiles each file and calls the compiled source files (object files) add.o and mult.o. It does not link these files; no executable program is created. The -c option is typically used to save useful functions for programs to be developed later. Once a function is in an object file it may be used as is, or saved in a library file and accessed like other library functions, as described in the following sections. Note that the cc command automatically creates an object file for each source file in the command line. Unless the -c option is given, however, it will also attempt to link these files, even if they do not form a complete program. 2-7

cc: C Compiler XENIX Programming Creati ng Programs from Object Files You can use the cc command to create executable programs from one or more object files, or from a combination of object files and C source files. The command compiles the source files (if any), then links the compiled source files with the object files to create an executable program. To create a program, give the names of the object and source files you wish to use. For example, if the source file main.c contains calls to the functions add and mult (in the object files add.o and mult.o), you can create a program by typing cc main.c add.o mult.o In this case, main.c is compiled and then linked with add.o and mult.o to create the executable file a.out. Linking a Program to Functions in Libraries You can link a progra m to functions in a library by using the -1 (for "library") option. The option directs cc to search the given library for the functions called in the source file. If the functions are found, the command links them to the program file. The option has the form cc -lname where name is a shortened version of the library's actual file name (see the XENIX 286 C Library Guide for a list of names). Spaces between the name and option are optional. The linker searches the /lib directory for the library. If not found, it searches the /usr/lib directory. For example, the command cc main.c -lcurses links the library /lib/Slibcurses.a to the source file main.c. A library is a convenient way to store a large collection of object files. The XENIX system provides several libraries, the most common of which is the standard C library. Functions in this library are automatically linked to your program whenever you invoke the compiler. Other libraries, such as curses, must be explicitly linked using the -1 option. The XENIX libraries and their functions are described in detail in the XENIX 286 C Library Guide. In general, the cc command does not search a library until the -1 option is encountered, so the placement of the option is important. The option must follow the na mes of any source files containing calls to functions in the given library. In general, all library options should be placed at the end of the command line, after all source and object files. 2-8

cc: C Compiler <strong>XENIX</strong> Programming<br />

Creati ng Programs from Object Files<br />

You can use the cc command to create executable programs from one or more object<br />

files, or from a combination of object files and C source files. The command compiles<br />

the source files (if any), then links the compiled source files with the object files to<br />

create an executable program.<br />

To create a program, give the names of the object and source files you wish to use. For<br />

example, if the source file main.c contains calls to the functions add and mult (in the<br />

object files add.o and mult.o), you can create a program by typing<br />

cc main.c add.o mult.o<br />

In this case, main.c is compiled and then linked with add.o and mult.o to create the<br />

executable file a.out.<br />

Linking a Program to Functions in Libraries<br />

You can link a progra m to functions in a library by using the -1 (for "library") option. The<br />

option directs cc to search the given library for the functions called in the source file.<br />

If the functions are found, the command links them to the program file.<br />

The option has the form<br />

cc -lname<br />

where name is a shortened version of the library's actual file name (see the <strong>XENIX</strong> <strong>2<strong>86</strong></strong><br />

C Library <strong>Guide</strong> for a list of names). Spaces between the name and option are optional.<br />

The linker searches the /lib directory for the library. If not found, it searches the<br />

/usr/lib directory.<br />

For example, the command<br />

cc main.c -lcurses<br />

links the library /lib/Slibcurses.a to the source file main.c.<br />

A library is a convenient way to store a large collection of object files. The <strong>XENIX</strong><br />

system provides several libraries, the most common of which is the standard C library.<br />

Functions in this library are automatically linked to your program whenever you invoke<br />

the compiler. Other libraries, such as curses, must be explicitly linked using the -1<br />

option. The <strong>XENIX</strong> libraries and their functions are described in detail in the <strong>XENIX</strong><br />

<strong>2<strong>86</strong></strong> C Library <strong>Guide</strong>.<br />

In general, the cc command does not search a library until the -1 option is encountered,<br />

so the placement of the option is important. The option must follow the na mes of any<br />

source files containing calls to functions in the given library. In general, all library<br />

options should be placed at the end of the command line, after all source and object<br />

files.<br />

2-8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!