the preprocessor and the compilation process cop3275 – programming using c diego j....

Post on 24-Dec-2015

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

The preprocessor and the compilation process

COP3275 – PROGRAMMING USING CDIEGO J. RIVERA-GUTIERREZ

Administrative stuff

• Homework #4 grades were released

• If you got a 20 with a comment about your code not compiling on this or any other homework: you NEED to go to office hours!! • Seriously, take a look at Canvas, make sure your grades reflect your effort. We are 2 weeks out of the end of the summer session. Don’t leave this to the last second.

• Homework #5 • Due Friday at 11:59pm

• Questions?

Quiz on Friday

• One theory question about files (explain one of the functions we covered)

• One theory question on the preprocessor (to be covered today)

• Write a function that does something regarding files.

• Understand what a short piece of code using the preprocessor would do (to be covered today)

The compilation process

How does the compilation process looks like?

source.c a.out (Binary file)

gcc source.c -o a.out

This was an oversimplification of the process…

This is actually a 3 stage process

• The first step is actually done by a subprogram of gcc which is called the preprocessor. (Which we will cover today)

• The second step is the actual compilation.

• The thirdstep is called “linking” which we will cover next Friday.

source.c

Preprocessor

Unlinked Machine

Code

“Preprocessed”

Source Compiler

a.out (Binary file)

Linker

Preprocessor

• The preprocessor handles mostly text changes • You can almost (almost) think about it as a find

and replace.

• Anything that starts with a # is a preprocessor directive.

•We have seen some in the past • #include• #define

#include

• We will talk more about include later. It has a lot to do with linking.

• But in general it brings another file and essentially copies it to your program.

• We will talk about how those files we’ve included look like later

• They are essentially a bunch of function declarations (not definitions).

#define

• Defines a constant.

#define <name> <value>

• Usually names are put in all upper case (by convention, but not necessary)

• I used it for ROWS and COLS in my HW3 and HW4 solutions.

• What happens is the preprocessor does a find and replace.

#define ROWS 9

• It can also do computations

• It can have new lines (\)

• It can also receive parameters and do some cool things.

Other functions we will cover

• #ifdef

• #endif

• #else

• #ifndef

• #if

• #elif

• __VA_ARGS__

Let’s do some examples

More on the preprocessor

Administrative stuff

• Homework #5 is due tonight.• Questions?

• We have 3 quizzes left• Today• July 31st in class• August 7th – take home will be assigned on August 5th. Due August 7th

at midnight.• Will have a chance for 5 extra points.

Homework #6

• Assignment will be posted tonight.

• Hangman• Read a dictionary of words from a file, set from console.• Receive seed from console.• Allow users to guess letters and play.• Upper case and lower case letters will be treated the same• Format for the “ASCII art” will be provided.• You’ll need to be able to print a alphabetically sorted version of the

dictionary.• You’ll need to organize your code in three files:• One for game play functions

• One for handling the dictionary

• One with your main function

Preprocessor

• Last Wednesday we talked about

• #define• Without• With one parameter• With newlines (\)• Let’s actually make one version with two parameters.• And one with variable parameters (__VA_ARGS__)

• #if

• #elif

• #else

• #endif

#ifdef and #ifndef

• This two are preprocessor checks to check if a macro is already defined

• Assume I say:

#define DIEGO

(notice that the meaning is optional, I can define a macro with no value)

• If I wanted to know if that is defined

#ifdef DIEGO

#endif

#ifdef and #ifndef

• Similarly if I wanted to check if something is not defined.

#ifndef DIEGO

#endif

• This is the actual if we use for operating systems.

If for detecting operating system

• _WIN32• Any modern Windows OS

• _WIN64• 64bit Windows OS (almost all Windows versions in the last 4 years,

but not earlier)

• __APPLE__• Mac systems

• __linux• Most Linux distribution

• __unix• Other Unix based operating systemss

Redifining a value

• What happens if I want to change the value of a macro

• We could write another #define, but does that work?• I believe this is a warning (but let’s test it)

• Actually the correct way would undefining the macro and then defining it again• #undef NULL

• This is generally not something you want. • Treating macros as variables is VERY dangerous, because it happens

during compilation time, but it is possible.• Remember macros do mostly text replacement.

top related