c++clibspatialindex

Is it possible to redefine a macro without the use of ‘undef’?


I am trying to make sense of the libspatialindex source code. Being new to c++, I have difficulty wrapping my head around the concept of macros. The library’s API wrapper for C, sidx_api.cc, directly and indirectly includes numerous headers two of which seemingly define the same macro, concerned with interfacing a dynamic library, without ‘undef’:

Tools.h

45 #if (defined _WIN32 || defined _WIN64 || defined WIN32 || defined WIN64) && !defined __GNUC__
46  #ifdef SIDX_DLL_EXPORT
47  #define SIDX_DLL __declspec(dllexport)
48  #else
49  #define SIDX_DLL __declspec(dllimport)
50  #endif
51 
52  // Nuke this annoying warning. See http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html
53 #pragma warning( disable: 4251 )
54 
55 #else
56  #define SIDX_DLL
57 #endif

sidx_export.h

29 #pragma once
30 
31 #ifndef SIDX_C_DLL  
32 #if defined(_MSC_VER)
33 # define SIDX_C_DLL __declspec(dllexport)
34 # define SIDX_DLL __declspec(dllexport)
35 #else
36 # if defined(USE_GCC_VISIBILITY_FLAG)
37 # define SIDX_C_DLL __attribute__ ((visibility("default")))
38 # define SIDX_DLL __attribute__ ((visibility("default")))
39 # else
40 # define SIDX_C_DLL
41 # define SIDX_DLL
42 # endif
43 #endif
44 #endif

I believe that redefining a macro without ‘undef’ is problematic, e.g., as discussed here and here. Am I missing something here? Thanks.


Solution

  • No macro is illegally redefined here. Some may be defined more than once with identical definitions, which is OK. It's non-identical definitions that constitute a problem.

    The DLL itself is always built with SIDX_DLL_EXPORT defined by the project settings. Code that uses the DLL is built without this macro being defined.

    The second header is a part of the DLL. It is always compiled with SIDX_DLL_EXPORT being defined. Thus the macros defined by it, if any, are always identical to those defined in the first header. Such identical redefinition does not constitute a problem.