{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

l06_6up - Global Variables CSci 1113 C/C for Scientists and...

Info icon This 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 Global variables are visible to multiple functions. Their scope is at the file level and not restricted to a single function. Their scope is the rest of the file. 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 We often want to distribute our C++ code across many files. 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. The solution is to put it in a separate file. 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 g++ SquareRootF.cpp mainSR1.cpp We can compile them all at once. 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) 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 We still had to put the declaration of mysqrt in mainSR1.cpp . With many functions this becomes problematic. We used header files for C libraries and want to write our own. 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
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern