CS 228 unit -*- Outline -*- * compiling C++ programs (DD 1.12-13, 1.14-16) ** compilers vs. interpreters to explain purpose of a compiler ------------------------------- COMPILERS vs. INTERPRETERS (Scheme program) -----------> output interpreter {C++ program} | | compiler | v [exectuable] ----------> output machine ------------------------------- Some interpreters (e.g. Chez Scheme) work by quickly compiling what you type then running it. But the general idea of an interpreter is that it is a high-level machine. ** phases of compilation (DD fig 1.1) The above suppresses the details of preprocessing and linking On Unix, g++ does preprocessing, compilation and linking *** preprocessing and compiling ------------------------------ IMPORTANT PHASES OF COMPILATION % g++ -ansi -pedantic -Wall prog.C {prog.C} | | preprocessor | v {prog.C with #include files and macros expanded} | | compiler proper | v [a.out] % ./a.out Welcome to the world of compilation! -------------------------------- there is also an assembly phase not shown, and probably others The -ansi and -pedantic flag turn off extensions to standard C++ The -Wall flag asks for all warnings. the compiler does the preprocessing invisibly to see how preprocessing works, use -E option and look at the prog.ii file which is created. Q: what would change if the program was named myprog.C? To get a different output file name, use the -o flag -------------------------------- % g++ -ansi -pedantic -Wall \ -o prog.exe prog.C % ./prog.exe Welcome to the world of compilation! -------------------------------- ------------------------- SHELL SCRIPT FOR CALLING g++ The command /home/cs228/public/bin/ansicpp can be used instead of g++ -ansi -pedantic -Wall -------------------------- it does the same thing. % which ansicpp -------------------------- % ansicpp -o prog.exe prog.C % ./prog.exe -------------------------- Show the code for prog.C Q: what would change if the program was named myprog.C? ** linking to demonstrate input, header files, and linking ----------------------------- PROBLEM Write a program to input a number and output its cosine. ------------------------------ be sure to explain everything as go along, but don't get bogged down in the C++ SNAPSHOT 1 // cosine-main.C int main() // MODIFIES: cin, cout // POST: cout has a prompt written to it, // cin has a number read from it, and // cout has the cosine of that number added to it. { // prompt for input // read num // write cosine of num } ////////////// for doing cosine, use man to find it, could also use book SNAPSHOT 2 // cosine-main.C int main() // MODIFIES: cin, cout // POST: cout has a prompt written to it, // cin has a number read from it, and // cout has the cosine of that number added to it. { prompt("Number? "); // read num double num; cin >> num; // write cosine of num cout << cos(num) << endl; } ////////// try compiling this. Show the errors. need to have a declaration for prompt, and for cin, cout, and endl. #include to get declarations for the io stuff give our own declaration of prompt Briefly explain the declaration concept: each name has a type, compiler has to know types of functions (like prompt) and variables (like cin) before they are used. SNAPSHOT 3 // cosine-main.C #include extern void prompt(char *promptstring); int main() // MODIFIES: cin, cout // POST: cout has a prompt written to it, // cin has a number read from it, and // cout has the cosine of that number added to it. { prompt("Number? "); // read num double num; cin >> num; // write cosine of num cout << cos(num) << endl; } ////////////// now get undefined symbol errors: cos and prompt. but we also need a declaration for cos, and need to write prompt. SNAPSHOT 4 // cosine-main.C #include #include extern void prompt(char *promptstring); int main() // MODIFIES: cin, cout // POST: cout has a prompt written to it, // cin has a number read from it, and // cout has the cosine of that number added to it. { prompt("Number? "); // read num double num; cin >> num; // write cosine of num cout << cos(num) << endl; } void prompt(char *promptstring) // MODIFIES: cout // POST: promptstring has been added to cout. { cout << promptstring; } ///////////// now just need to link with -lm flag to get cosine. ---------------------------- HEADER FILES and LINKING SUMMARY % ansicpp -o cosine.exe cosine-main.C -lm /usr/local/lib/g++-include/iostream.h math.h {cosine-main.C} | | preprocessor | v {cosine-main.C with iostream.h and math.h} | | compiler proper | v {object file} and /usr/local/lib/libm.a | | linking loader | v [cosine.exe] ---------> output machine % ./cosine.exe Number? 3.14159 -1 ---------------------------- point out connection between -lm and libm.a Q: questions about this? ** separate compilation (if time) If you have a useful function like prompt SNAPSHOT 5 // cosine-main.C #include #include extern void prompt(char *promptstring); int main() // MODIFIES: cin, cout // POST: cout has a prompt written to it, // cin has a number read from it, and // cout has the cosine of that number added to it. { prompt("Number? "); // read num double num; cin >> num; // write cosine of num cout << cos(num) << endl; } // prompt.C #include void prompt(char *promptstring) // MODIFIES: cout // POST: promptstring has been added to cout. { cout << promptstring; } /////// compile with ansicpp -o cosine.exe prompt.C cosine-main.C -lm and with ansicpp -c prompt.C ansicpp -c cosine-main.C ansicpp -o cosine.exe prompt.o cosine-main.o -lm Why do this, so can change cosine-main without recompiling prompt. Edit it to have it print "The cosine of " << num << " is ". ** making own header file make prompt.h, put specs in it, and include it in cosine-main.C and in prompt.C (include it there so catch errors earlier). ----------------------------------- SEPARATE COMPILATION SUMMARY % ansicpp -c cosine-main.C /usr/local/lib/g++-include/iostream.h math.h ./prompt.h {cosine-main.C} | | preprocessor | v {cosine-main.C with iostream.h, math.h and prompt.h} | | compiler proper | v cosine-main.o % ansicpp -o cosine.exe prompt.o \ cosine-main.o -lm prompt.o, cosine-main.o and /usr/local/lib/libm.a | | linking loader | v [cosine.exe] ---------> output machine ----------------------------------- Q: what if our program also involved graphics.C and graphics.h? ** for more info see $PUB/doc/running-c++.txt, man page for g++ we'll save makefiles for later