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 You can execute the new program by typing a.out The program waits until you enter two numbers, then prints their sum. For example, if you type "3 5" the program displays "8". Compiling Several Source Files Large source programs are often split into several files to make them easier to understand, update, and edit. You can compile such a program by giving the names of all the files belonging to the program when you invoke the cc command. The command reads and compiles each file in turn, then links all object files together, and copies the new program to the file a.out. To compile several source files, type cc filename • • • where each filename is separated from the next by at least one space. One of these files (and only one) must contain a "main" function. The others may contain functions called by this "main" function or by other functions in the program. The files must not contain calls to functions not explicitly defined by the program or by the standard C library. For example, suppose the following main program function is stored in the file main. #include extern int add(); main () { int x,y,z; } scanf ("%d %d", &x, &y); z = add (x, y); printf (" %d \n", z); Assume that the following function is stored in the file add.c. add (a, b) int a, b; { return (a + b); } You can compile these files and create an executable program by typing cc main.c add.c 2-3

cc: C Compiler XENIX Programming The command compiles the statements in main.c, then compiles the statements in add.c. Finally, it links the two together (along with the standard C library) and copies the program to a.out. This program, like the program in the previous section, waits for two numbers, then prints their sum. Since the cc command cannot keep track of more than one compiled file at a time, when several source files are compiled at a time, the command creates object files to hold the binary code generated for each source file. These object files are then linked to create an executable program. The object files have the same base name as the source files but are given the ".o" file extension. For example, when you compile the two source files above, the compiler produces the object files main.o and add.o. These files are permanent files, i.e., the command does not delete them after completing its operation. Note that the command also creates an object file if only one source file is compiled. Naming the Output File You can give the executable program file any valid file name by using the -o (for "output") option. The option has the form -o filename where filename is a valid file name or path name. If a file name is given, the program file is stored in the current directory. If a full path name is given, the file is stored in the given directory. If that file already exists, its contents are replaced with the new executable program. For example, the command cc -o addem main.c add.o causes the compiler to create an executable program file addem from the source file main.c and object file add.o. You can execute this program by typing add em Note that the -o option does not affect the existing a.out file. This means that the cc command does not change the current contents of a.out if the -o option has been given. Compiling Programs for the MS-DOS Environment The XENIX C compiler can compile programs that will operate in either the MS-DOS or the XENIX environment. However, to compile a program for the MS-DOS environment, you must use a different set of standard libraries and a different linker. The -dos option instructs the compiler to alter the standard search path and to search the appropriate libraries for the MS-DOS environment. Note that programs compiled with these libraries will not run in the XENIX environment. 2-4

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

The command compiles the statements in main.c, then compiles the statements in add.c.<br />

Finally, it links the two together (along with the standard C library) and copies the<br />

program to a.out. This program, like the program in the previous section, waits for two<br />

numbers, then prints their sum.<br />

Since the cc command cannot keep track of more than one compiled file at a time, when<br />

several source files are compiled at a time, the command creates object files to hold<br />

the binary code generated for each source file. These object files are then linked to<br />

create an executable program. The object files have the same base name as the source<br />

files but are given the ".o" file extension. For example, when you compile the two<br />

source files above, the compiler produces the object files main.o and add.o. These files<br />

are permanent files, i.e., the command does not delete them after completing its<br />

operation. Note that the command also creates an object file if only one source file is<br />

compiled.<br />

Naming the Output File<br />

You can give the executable program file any valid file name by using the -o (for<br />

"output") option. The option has the form<br />

-o filename<br />

where filename is a valid file name or path name. If a file name is given, the program<br />

file is stored in the current directory. If a full path name is given, the file is stored in<br />

the given directory. If that file already exists, its contents are replaced with the new<br />

executable program.<br />

For example, the command<br />

cc -o addem main.c add.o<br />

causes the compiler to create an executable program file addem from the source file<br />

main.c and object file add.o. You can execute this program by typing<br />

add em<br />

Note that the -o option does not affect the existing a.out file. This means that the cc<br />

command does not change the current contents of a.out if the -o option has been given.<br />

Compiling Programs for the MS-DOS Environment<br />

The <strong>XENIX</strong> C compiler can compile programs that will operate in either the MS-DOS or<br />

the <strong>XENIX</strong> environment. However, to compile a program for the MS-DOS environment,<br />

you must use a different set of standard libraries and a different linker.<br />

The -dos option instructs the compiler to alter the standard search path and to search<br />

the appropriate libraries for the MS-DOS environment. Note that programs compiled<br />

with these libraries will not run in the <strong>XENIX</strong> environment.<br />

2-4

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

Saved successfully!

Ooh no, something went wrong!