Intel XENIX 286 Programmers Guide (86) - Tenox.tc
Intel XENIX 286 Programmers Guide (86) - Tenox.tc Intel XENIX 286 Programmers Guide (86) - Tenox.tc
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
- Page 1 and 2: 0 • • •
- Page 3 and 4: The information in this document is
- Page 5 and 6: Table of Contents CONTENTS Compiler
- Page 7 and 8: Table of Contents CONTENTS Using Ot
- Page 9 and 10: Table of Contents CONTENTS Assemble
- Page 11 and 12: Table of Contents CONTENTS CHAPTER
- Page 13 and 14: Table of Contents TABLES TABLE TITL
- Page 15 and 16: Introduction XENIX Programming 10.
- Page 17 and 18: cc: C Compiler XENIX Programming Cr
- Page 19 and 20: cc: C Compiler XENIX Programming Th
- Page 21: cc: C Compiler XENIX Programming Th
- Page 25 and 26: cc: C Compiler XENIX Programming Th
- Page 27 and 28: cc: C Compiler XENIX Programming Co
- Page 29 and 30: cc: C Compiler XENIX Programming Sa
- Page 31 and 32: cc: C Compiler XENIX Programming Us
- Page 33 and 34: cc: C Compiler XENIX Programming Wh
- Page 35 and 36: cc: C Compiler XENIX Programming Co
- Page 37 and 38: cc: C Compiler XENIX Programming d
- Page 39 and 40: cc: C Compiler XENIX Programming Se
- Page 41 and 42: lint: C Program Checker XENIX Progr
- Page 43 and 44: lint: C Program Checker XENIX Progr
- Page 45 and 46: lint: C Program Checker XENIX Progr
- Page 47 and 48: lint: C Program Checker XENIX Progr
- Page 49 and 50: lint: C Program Checker XENIX Progr
- Page 51 and 52: lint: C Program Checker XENIX Progr
- Page 53 and 54: make: Program Maintainer XENIX Prog
- Page 55 and 56: make: Program Maintainer XENIX Prog
- Page 57 and 58: make: Program Maintainer XENIX Prog
- Page 59 and 60: make: Program Maintainer XENIX Prog
- Page 61 and 62: make: Program Maintainer XENIX Prog
- Page 63 and 64: make: Program Maintainer XENIX Prog
- Page 65 and 66: make: Program Maintainer print: $(F
- Page 67 and 68: SCCS: Source Code Control System XE
- Page 69 and 70: SCCS: Source Code Control System XE
- Page 71 and 72: SCCS: Source Code Control System XE
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