Explain Preprocessing

3 replies [Last post]
Isaac Yates
Isaac Yates's picture
Title: NooBot
Joined: 12/14/2013
Posts:
BotPoints: 19
User offline. Last seen 7 years 25 weeks ago.

Hi,

I am confused about a c programming concept. It has to do with the #ifndef and the #define. I understand that #ifndef DEFINITION checks to see if there are any previous #define DEFINITIONs called. My question is, why is this useful? An example I have run across was written by a retired team member:

#ifndef __OPENCODE_H__
#define __OPENCODE_H__

the header file then proceeds to include a number of headers that make up opencode, and then ends the file with a #endif. Here is the whole header file:

#ifndef __OPENCODE_H__
#define __OPENCODE_H__
#ifndef __arm__
#include "../config/cbcconfig.h"
#endif
#include "opencode/common/depthlib.h"
#include "opencode/cbc/drive/drivelib.h"
#include "opencode/cbc/light/lightstart.h"
#include "opencode/cbc/sensor/sensorlib.h"
#include "opencode/cbc/sensor/link_depth.h"
#include "opencode/cbc/servo/servolib.h"
#include "opencode/create/create_accel.h"
#include "opencode/create/create_config.h"
#include "opencode/create/create_drive.h"
#include "opencode/create/create_music.h"
#include "opencode/create/create_script.h"
#include "opencode/create/create_sensor.h"
#include "opencode/create/create_align.h"
#include "opencode/create/create_depth.h"
#endif

From what I can tell it has to do with something called preprocessing? Can someone explain to me importance (or unimportance) of defining __OPENCODE_H__ at all. I'd like to be able to intelligently write my own libs in the future (and teach others how to), and this seems important. Thanks.

“I have never let my schooling interfere with my education.” ~Mark Twain

Isaac Yates
Isaac Yates's picture
Title: NooBot
Joined: 12/14/2013
Posts:
BotPoints: 19
User offline. Last seen 7 years 25 weeks ago.

Sorry for the double post. That was an accident. I don't know how to delete the other.

“I have never let my schooling interfere with my education.” ~Mark Twain

bonnienichols
Title: NooBot
Joined: 03/07/2014
Posts:
BotPoints: 52
User offline. Last seen 6 years 25 weeks ago.

Yes, I understood.Being an IT graduate, I can also see that this preprocessor has nothing to do with the OPENCODE.

ruby fortune review

ruler501
ruler501's picture
Title: NooBot
Joined: 01/29/2012
Posts:
BotPoints: 367
User offline. Last seen 7 years 20 weeks ago.

Basically what this is called is an include guard. It makes sure that the file get's included only once. The basic idea is that if you include it once then it is defined so it won't get past the #ifndef