5- C++ environment(1).pdf
Document Details
Uploaded by CalmSerpentine8789
Tags
Full Transcript
C++ Unix Environment C++ Program Structure Comment block – Filename, author, date, purpose, description, … Preprocessor directives #include … Using statements using namespace std; Main function int main () { Declarations Statements...
C++ Unix Environment C++ Program Structure Comment block – Filename, author, date, purpose, description, … Preprocessor directives #include … Using statements using namespace std; Main function int main () { Declarations Statements return 0; } C++ Basics Comments are for humans and are ignored by the compiler – // rest of line after double slash is ignored – Include directives load predefined code interfaces so compiler “knows” about functions, classes, and other predefined entities – #include loads C++ header from “normal” location Headers define entities within namespaces – #include loads C-style header from “normal” location No namespaces – #include “myStuff.h” Loads user-defined header from current directory May or may not contain namespaces Simple C++ Program (helloWorld.cpp) // file: helloWorld.cpp // author: M. Amine Belkoura // date: 02/06/2015 // purpose: CS3376 // description: // this prints a welcome message to the screen #include using namespace std; int main( ) { cout./HelloWorld Hello World! g++ Syntax and Behavior Why 1-step process + Simple - Easy for one source file Why 2-step process + Easy for debugging + Modular + Great for multiple source files - longer commands In this class: – 2-step building The Process main.cpp 1 1: g++ -c main.cpp main.o 2 2: g++ -o main main.o main The Process main.cpp file1.cpp 1: g++ -c main.cpp 1 2 2: g++ -c file1.cpp main.o file1.o 3 3: g++ -o main main.o file1.o main The Process 1: g++ -c main1.cpp file.cpp main2.cpp 2: g++ -c file.cpp main1.cpp 3: g++ -c main2.cpp 1 2 3 main1.o file.o main2.o 4 4: g++ -o main1 main1.o file.o 5 main1 3: g++ -o –lmath main2 main2.o file.o main2 5 Libraries Options For g++ Compilation “-c”: Compiles.cpp file arguments to.o but does not link. “-g”: Generates debugging information that is used by gdb-based debuggers “-I”: Adds the directory to the list of directories searched for include files. Compilation Options, cont. “-Wall”: Directs g++ to give warnings about code that’s probably in error. For example, it will catch and report: int number = GetRandomInteger() if (number = 6) cout