I. compiling C++ programs (DD 1.12-13, 1.14-16) A. compilers vs. interpreters ------------------------------- COMPILERS vs. INTERPRETERS (Scheme program) -----------> output interpreter {C++ program} | | compiler | v [exectuable] ----------> output machine ------------------------------- B. phases of compilation (DD fig 1.1) 1. 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! -------------------------------- what would change if the program was named myprog.C? -------------------------------- % 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 -------------------------- -------------------------- % ansicpp -o prog.exe prog.C % ./prog.exe -------------------------- what would change if the program was named myprog.C? C. linking ----------------------------- PROBLEM Write a program to input a number and output its 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 ---------------------------- questions about this? D. separate compilation (if time) E. making own header file ----------------------------------- 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 ----------------------------------- what if our program also involved graphics.C and graphics.h? F. for more info