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

Discussion in 'Programmer's Corner' started by integral, Jan 7, 2011.

  1. integral

    Thread Starter New Member

    Aug 19, 2010
    22
    0
    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)

    :

    Code ( (Unknown Language)):
    1.  
    2.    
    3. #include <cv.h>
    4. #include <highgui.h>
    5. #include <ctype.h>
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8.  
    9. #include <iostream>
    10. #include <vector>
    11.  
    12. using namespace std;
    13.  
    14.  
    15. // define whether to use approximate nearest-neighbor search
    16. [COLOR=Red]#define USE_FLANN[/COLOR]
    17.  
    18.  
    19. IplImage *image = 0;
    20.  
    21. double
    22. compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
    23. {
    24.     double total_cost = 0;
    25.     assert( length % 4 == 0 );
    26.     for( int i = 0; i < length; i += 4 )
    27.     {
    28.         double t0 = d1[i] - d2[i];
    29.         double t1 = d1[i+1] - d2[i+1];
    30.         double t2 = d1[i+2] - d2[i+2];
    31.         double t3 = d1[i+3] - d2[i+3];
    32.         total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
    33.         if( total_cost > best )
    34.             break;
    35.     }
    36.     return total_cost;
    37. }
    38.    
    39.    [/i][/i]
    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?
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Lower in the code you should see something along the lines of:
    Code ( (Unknown Language)):
    1.  
    2. ifdef USE_FLANN
    3. {
    4. ...
    5. }
    6. else
    7. {
    8. ...
    9. }
    10.  
    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.
     
  3. integral

    Thread Starter New Member

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

    Here is the section of code later in the file:
    Code ( (Unknown Language)):
    1. #ifdef USE_FLANN
    2.     flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
    3. #else
    4.     findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
    5. #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 ?
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
Loading...