1

I'm using QuickFAST library and while checking it I found this class declaration which I don't seem to really get ! I mean what does a macro name before the class name !

class QuickFAST_Export Message : public FieldSet

also I found this declaration

friend void QuickFAST_Export intrusive_ptr_add_ref(const Field * ptr);

and again I don't get the use of this declaration !

for more info here's the QuickFAST_Export.hpp

#ifdef _MSC_VER
# pragma once
#endif
#ifndef QUICKFAST_EXPORT_H
#define QUICKFAST_EXPORT_H

// Compile time controls for library generation.  Define with /D or #define
// To produce or use a static library: #define QUICKFAST_HAS_DLL=0
//   Default is to produce/use a DLL
// While building the QUICKFAST_ library: #define QUICKFAST_BUILD_DLL
//   Default is to export symbols from a pre-built QUICKFAST DLL
//
// Within QUICKFAST use the QuickFAST_Export macro where a __declspec is needed.

#if defined (_WIN32)

#  if !defined (QUICKFAST_HAS_DLL)
#    define QUICKFAST_HAS_DLL 1
#  endif /* ! QUICKFAST_HAS_DLL */

#  if defined (QUICKFAST_HAS_DLL) && (QUICKFAST_HAS_DLL == 1)
#    if defined (QUICKFAST_BUILD_DLL)
#      define QuickFAST_Export __declspec(dllexport)
#    else /* QUICKFAST_BUILD_DLL */
#      define QuickFAST_Export __declspec(dllimport)
#    endif /* QUICKFAST_BUILD_DLL */
#  else /* QUICKFAST_HAS_DLL == 1 */
#    define QuickFAST_Export
#  endif /* QUICKFAST_HAS_DLL == 1 */

#  else /* !_WIN32 */
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joy
  • 1,707
  • 7
  • 29
  • 44

2 Answers2

2

It means that the class is either exported or imported, depending on which module is built.

If QUICKFAST_HAS_DLL is defined and equal to 1, it means that the module is built as a DLL. To use functionalities from the outside, classes and methods have to be exported.

Inside the module, QUICKFAST_BUILD_DLL is defined. So when you build the module, QuickFAST_Export expands to __declspec(dllexport). Your class definition becomes:

class __declspec(dllexport) Message : public FieldSet

When you include the header from a different module, QUICKFAST_BUILD_DLL is not defined, so the macro expands to __declspec(dllimport), and your class definition to:

class __declspec(dllimport) Message : public FieldSet
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • ok I got it now , but putting the __declspec(dllimport) before className what is supposed to do to the body of my class, I mean I still don't get this declaration , I know that for déclaring a class all we need to do is "class className : public ClassBase" , so what is this new declaration? – Joy Apr 18 '12 at 14:06
  • @Glolita have you tried googling the syntax? It tells the linker that class is to be exported. – Luchian Grigore Apr 18 '12 at 14:08
  • @Glolita I have exactly the same question. People don't seem to answer my (our) question satisfactorily. I guess my real question is, what actual thing is `__declspec(dllimport)` exactly, and how is it legal syntax in C++ to put it in the middle of a class declaration? So I'm afraid @LuchianGrigore's comment doesn't help at all. – Ray Dec 04 '15 at 15:44
2

The macro expands to either __declspec(dllimport) or __declspec(dllexport), depending if the class is exported from the DLL or imported on the other side.

dwo
  • 3,576
  • 2
  • 22
  • 39