3

I know the following 3 parts #define:

#define PI 3.4

which mean it will replace PI with 3.4.

But that's the meaning of 2 parts #define like this:

#define something

Will it replace something with null/empty string?

The following is the code example, I searched the file, only list the related lines

  D:\mariadb\storage\pbxt\src\cache_xt.cc (23 hits)  
    Line 172: #ifdef xtPublic  
    Line 173: #undef xtPublic  
    Line 188: #define xtPublic  
    Line 325: xtPublic XTIndHandlePtr xt_ind_get_handle(..)  
    Line 378: xtPublic void xt_ind_release_handle(XTIndHandlePtr..)  
    Line 516: xtPublic xtBool xt_ind_copy_on_write(XTIndReferencePtr iref)  
    Line 597: xtPublic void xt_ind_lock_handle(XTIndHandlePtr handle) 
Sriram
  • 10,298
  • 21
  • 83
  • 136
mysql guy
  • 245
  • 1
  • 8

5 Answers5

14

Yes it meaning replace something with an empty string. But the important thing is now something is recognized by the preprocessor that it is "defined", so

#ifdef something

will pass after that #define (Line 172).

Also, it is common to use it for configurational or vendor-specific attributes (Line 325, ...), like

#if MSVC
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

EXPORT void f();
// expand to '__declspec(dllexport) void f()' in MSVC
// expand to 'void f()' in other compilers
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

Those declarations are usually given within header files, as a means of preventing double inclusion of the same file. These are also called include guards.

Sriram
  • 10,298
  • 21
  • 83
  • 136
1

#define something will result into something just defined. It will not cause a compiler error. It is used usually like

void getValue(IN int& x, OUT int& y). If you do #define IN and #define OUT it will not give a compiler error and anybody will get to know x is input and y is output

One more use is like

#ifndef __ABC_H__
#define __ABC_H__

...

#endif

This is to prevent reinclusion of for eg. "abc.h"

Mayank
  • 5,454
  • 9
  • 37
  • 60
  • Of course, we wouldn't dare to actually use `__ABC_H__` as an identifier, because [it's reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – GManNickG May 25 '11 at 09:10
1

Its is nothing but Pre-Processor Directive, the #define just will direct the Header files to the considered Library files or can declares the constants.

1

Yes, it replaces the preprocessor with empty string. It helps is self-documenting the code without writing lengthy comments.

Naveen
  • 74,600
  • 47
  • 176
  • 233