Need help understanding “#define” usage in C++

Thread Starter

integral

Joined Aug 19, 2010
22
I am new to learning C++ and one thing I have been doing to learn is to read through code others have written. I am interesting in image processing so I have being working with OpenCV.
My question has to do specifically with the usage of the pre-processor directive “#define”
It ‘s usage is defined as:

#define identifier replacement

Example:
#define THRESHOLD 100

The above replaces any occurrence of the word THRESHOLD with 100.
Sometimes I see #define used without a replacement. For example ("#define USE_FLANN" in the following)

:

Rich (BB code):
#include <cv.h>
#include <highgui.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <vector>

using namespace std;


// define whether to use approximate nearest-neighbor search
#define USE_FLANN


IplImage *image = 0;

double
compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
{
    double total_cost = 0;
    assert( length % 4 == 0 );
    for( int i = 0; i < length; i += 4 )
    {
        double t0 = d1 - d2;
        double t1 = d1[i+1] - d2[i+1];
        double t2 = d1[i+2] - d2[i+2];
        double t3 = d1[i+3] - d2[i+3];
        total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
        if( total_cost > best )
            break;
    }
    return total_cost;
}
   
   
The file where this segment of code comes from can be found here: http://sharesend.com/50nu3
What happens when #define is used without a replacement and why would one want to do that?
 

thatoneguy

Joined Feb 19, 2009
6,359
Lower in the code you should see something along the lines of:
Rich (BB code):
ifdef USE_FLANN
{
...
}
else
{
...
}
When a define defines a constant, such as LED=PORTB.1, the first pass of the compiler (pre-processor) simply goes through the code and everwhere LED appears as a variable, PORTB.1 is substituted.

The define in your example is a boolean, and is usually tested with ifdef, which is also handled during the pre processor pass of the compiler, allowing a single file of source code to be written for several different processors or peripherals. Only the code blocks that match the define will be included in the actual build.
 

Thread Starter

integral

Joined Aug 19, 2010
22
I am not sure I understood you correctly.

Here is the section of code later in the file:
Rich (BB code):
#ifdef USE_FLANN
    flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
    findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
If I understood you correctly the #define USE_FLANN is just used to determine which of the functions will be used. Therefore depending on what your application of the software is one can either leave or comment out the #define USE_FLANN ?
 

thatoneguy

Joined Feb 19, 2009
6,359
I am not sure I understood you correctly.

Here is the section of code later in the file:
Rich (BB code):
#ifdef USE_FLANN
    flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
    findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
If I understood you correctly the #define USE_FLANN is just used to determine which of the functions will be used. Therefore depending on what your application of the software is one can either leave or comment out the #define USE_FLANN ?
Correct.

Only the code that matches the defines will actually be compiled. Such as a block of code that takes advantage of a floating point unit, if available, else it uses library routines for math.
 
Top