DOC PREVIEW
UI CS 270 - C Programming Tools

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C Programming ToolsRead Chapter 11Linux typically comes with compilersGNU C (gcc) (the old cc compiler is linked to gcc)GNU C++ (g++)1C Programming ToolsUtility: gcc -cv [ -o fileName ] [ -pg ] { fileName }*gcc compiles C program code in one or more files and produces object modules or an executable file. Files specified should have a ".c" extension. -c option to produce object modules suitable for linking later-o option to specify a filename other than the default "a.out" -pg option to produce profiling data for the GNU profiler gprof. -v option to produce verbose commentary during the compilation and/or linking process.2C Programming ToolsSeparately compiling and linkingusing gcc with -c option allows us to compile files separatelythe output are .o files$ gcc -c reverse.c ...compile reverse.c to reverse.o.$ gcc -c main1.c ...compile main1.c to main1.o.$ ls -lG reverse.o main1.o-rw-r--r-- 1 ables 311 Jan 5 18:08 main1.o-rw-r--r-- 1 ables 181 Jan 5 18:08 reverse.o$ _Alternatively, you can place all of the source code files on one line:$ gcc -c reverse.c main1.c ...compile each .c file to .o file.$ _3C Programming ToolsLinking .o files into an executablewe can use gcc for that$ gcc reverse.o main1.o -o main1 ...link object modules.note: in unix environment one often uses the stand-alone linking loader (ld) to link separate modules.gcc can do the sameuse -v option to see how gcc works4Multimodule ProgramsMotivationassume reverse program from book is to be used in other programse.g., use reverse to check for palindromecould copy-and-paste tedious if we wanted to change the function: would have to change every instant of the function, besidescopy-and-paste operation is tediouswaste of disk space5Multimodule ProgramsReusable Functionsremove function of interest from program compile separatelylink resultant object code to programs that want to us it6Multimodule ProgramsPreparing a reusable functionCreate a source code module that contains the source code of the function Create header file that contains the function's prototype. Then compile it into an object module by using the -c option of gcc. An object module contains machine code together with symbol-table information that allows it to be combined with other object modules when an executable file is being created.recall the -c option means Compile or assemble the source files, but do not link. 7Multimodule Programsexample reverse.h1 /* REVERSE.H */23 int reverse (); /* Declare but do not */ /* define this function */8Multimodule Programsexample reverse.c1 /* REVERSE.C */23 #include <stdio.h>4 #include "reverse.h"56 /****************************************************************/78 reverse (before, after)910 char *before; /* A pointer to the original string */11 char *after; /* A pointer to the reversed string */1213 {14 int i;15 int j;16 int len;1718 len = strlen (before);1920 for (j = len - 1, i = 0; j >= 0; j--, i++) /* Reverse loop */21 after[i] = before[j];2223 after[len] = 0; /* terminate reversed string */24 }9Multimodule Programsexample main1.c1 /* MAIN1.C */23 #include <stdio.h>4 #include "reverse.h" /* Contains the prototype of reverse () */56 /*************************************************************/78 main ()910 {11 char str [100];1213 reverse ("cat", str); /* Invoke external function */14 printf ("reverse (\"cat\") = %s\n", str);15 reverse ("noon", str); /* Invoke external function */16 printf ("reverse (\"noon\") = %s\n", str);17 }10Multimodule ProgramsNow compile$ gcc -c reverse.c main1.c ...compile each .c file to .o file.link$ gcc reverse.o main1.o -o main1 ...link object modules.and execute$ ./main1 ...run the executable.reverse ("cat") = tacreverse ("noon") = noon$ _11Multimodule ProgramsExample: check whether word is a palindromeexample palindrome.h1 /* PALINDROME.H */23 int palindrome (); /* Declare but do not define */12Multimodule Programsexample palindrome.c1 /* PALINDROME.C */23 #include "palindrome.h"4 #include "reverse.h"5 #include <string.h>67 /**************************************************************/89 int palindrome (str)1011 char *str;1213 {14 char reversedStr [100];15 reverse (str, reversedStr); /* Reverse original */16 return (strcmp (str, reversedStr) == 0); /* Compare the two */17 }13Multimodule Programsexample main2.c1 /* MAIN2.C */23 #include <stdio.h>4 #include "palindrome.h"56 /**************************************************************/78 main ()910 {11 printf ("palindrome (\"cat\") = %d\n", palindrome ("cat"));12 printf ("palindrome (\"noon\") = %d\n", palindrome ("noon"));13 }14Multimodule Programsrun$ gcc -c palindrome.c ...compile palindrome.c to palindrome.o.$ gcc -c main2.c ...compile main2.c to main2.o.$ gcc reverse.o palindrome.o main2.o -o main2 ...link them.$ ./main2 ...run the program.palindrome ("cat") = 0palindrome ("noon") = 1$ _15Archiving Modules: arUtility: ar key archiveName { fileName }*ar allows you to create and manipulate archives. The archive file should end with a ".a" suffix. key may be:d - deletes a file from an archiveq - appends file to archive, even if it's already presentr - adds a file to an archive if it isn't already there, or replaces the current version if it iss - builds index (table of contents) of library for faster accesst - displays an archive's table of contents to standard outputx - copies a list of files from an archive into the current directoryv - generates verbose output16makeThe rules in a make file tells make how to execute commands to build a target file from source files. It also specifies a list of dependencies of the target file.Make files can contain comments. Comments start with a # and are used to describe what is happening in the makefile or to hide definitions from17makeUtility: make [ -f makefile ]make is a utility that updates a file based on a series of dependency rules stored in a special format "make file". The -f option allows you to specify your own make filename if none is specified, make will look for the files "GNUmakefile," "makefile," and "Makefile," in that order.18makeFigure 11-9. make dependency specification.targetList:dependencyListcommandListtargetList is a list of target files dependencyList is a list of files that the files in targetList depend on. commandList is a list of


View Full Document

UI CS 270 - C Programming Tools

Download C Programming Tools
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Programming Tools and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Programming Tools 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?