l06_6up - : Global Variables : CSci 1113 C/C+ for...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
: CSci 1113 C/C++ for Scientists and Engineers Section 010 - evening class Functions, Continuted Prof. Eric Van Wyk and Ted Kaminski Spring 2010 c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 1 / 32 Global Variables : Global Variables I Global variables are visible to multiple functions. Their scope is at the file level and not restricted to a single function. I Their scope is the rest of the file. I See GlobalSumCounter.cpp c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 2 / 32 Using multiple C++ files : Multiple C++ files I We often want to distribute our C++ code across many files. I For example, if we have many applications that need our mysqrt function we don’t want to write it multiple times - once for each app. I The solution is to put it in a separate file. I Consider SquareRootF.cpp and mainSR1.cpp c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 3 / 32 Using multiple C++ files : Compiling multiple files I g++ SquareRootF.cpp mainSR1.cpp We can compile them all at once. I Or g++ -c SquareRootF.cpp g++ SquareRootF.o mainSR1.cpp We compile them separately — generating the object file ( .o file) for SquareRootF and then compiling the main program and linking the results together. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 4 / 32 Using multiple C++ files : Compiling multiple files (2) I Or g++ -c SquareRootF.cpp g++ -c mainSR1.cpp g++ SquareRootF.o mainSR1.o Here we compile the files separately, generating two object files. We then link them in the third step. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 5 / 32 Using multiple C++ files : Header files I We still had to put the declaration of mysqrt in mainSR1.cpp . I With many functions this becomes problematic. I We used header files for C libraries and want to write our own. I First, consider PowerNoHeader.cpp . Here we don’t include cmath.h and compilation still works. This is because the compile will link the programmers code to the standard libraries automatically if needed. c ± Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 6 / 32
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using multiple C++ files : Header files (2) I We put function declarations (also called function headers) in files with a .h extension and then include them. I See SquareRootF.h and mainSR2.cpp . I Files included using names in double quotes are looked for locally, not in the system libraries. I The contents of the included files are placed into the file, replacing the include , and then compiled.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/28/2010 for the course CSCI 1113 taught by Professor Staff during the Spring '08 term at Minnesota.

Page1 / 6

l06_6up - : Global Variables : CSci 1113 C/C+ for...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online