2

I can analyzing a piece of code which is as follows:

class DPD_API gain_phase_calibrate : virtual public gr::block
{
public:
    typedef boost::shared_ptr<gain_phase_calibrate> sptr;

    static sptr make();
};

From this code, I understand that a class called DPD_API is being created and it is inheriting from the gr::block class. What I dont understand is the gain_phase_calibrate. Is this a function name or a class name? Can anyone please explain this to me?

Mobi Zaman
  • 605
  • 1
  • 6
  • 19
  • 5
    It can not be answered for sure, without more information, but it is very likely that gain_phase_calibrate is the class name and DPD_API is some macro. – gerum Apr 12 '23 at 08:07
  • 5
    `DPD_API` is probably a *macro* to make the class exported (or imported) from a DLL. – Some programmer dude Apr 12 '23 at 08:08
  • 1
    Thanks for the information. That is probably the case. I found this line in another part of the code. #define DPD_API __GR_ATTR_EXPORT – Mobi Zaman Apr 12 '23 at 08:11
  • I would guess `DPD_API` is very much used as in this question [Macro for dllexport/dllimport switch](https://stackoverflow.com/questions/14980649/macro-for-dllexport-dllimport-switch). – Friedrich Apr 12 '23 at 08:11
  • Now my next question would be that why are macros used for the class definition. What do they assist with? Can anyone point me to an article explaining this? – Mobi Zaman Apr 12 '23 at 08:12
  • @MobiZaman It's to do with exporting items from a DLL (or conversely importing items from a DLL). No doubt you can find a full explanation on the Microsoft site. – john Apr 12 '23 at 08:14
  • 4
    @MobiZaman Here you go, https://learn.microsoft.com/en-us/cpp/cpp/dllexport-dllimport?view=msvc-170 – john Apr 12 '23 at 08:15
  • 3
    If you keep looking for where `__GR_ATTR_EXPORT ` is defined, you will find that it expands to some platform-specific attributes. Then read about what those mean. – molbdnilo Apr 12 '23 at 08:17

1 Answers1

3

The code snippet declares an API class called gain_phase_calibrate which inherits from gr::block. Here, DPD_API is the class' optional attributes, see class declaration on cppreference.

The DPD_API macro will expand to platform and compiler specific code which controls the visibility of the class, i.e. whether it can be called from outside of the library.

In plain English, this code says "here's a class called gain_phase_calibrate and you can call it from your code if you use this library."

These macros are a common sight in C++ libraries. Conventionally, they are called FOO_EXPORT or FOO_API (as in this case).

Historically, shared objects on *NIX made all symbols visible while Microsoft's DLLs exposed only those with the __declspec(dllexport) macro. In the mean time, selectively exposing API symbols also caught on on *NIX.

A good summary for GCC (and comparison with the situation on Windows) can be found here in the GCC wiki on Visibiliy. It also features some sample code not unlike the one in the question.

Friedrich
  • 2,011
  • 2
  • 17
  • 19