1

I am trying to understand some code that I have inherited, but am having some trouble googling my way out of it, and have some questions I hope some of you can help me with. The code can be seen just below, and is a .hpp (c++ header file) which declares a function.

#ifdef _MSC_VER
    #define EXPORT_SYMBOL __declspec(dllexport)
#else
    #define EXPORT_SYMBOL
#endif

#ifdef __cplusplus
extern "C" {
#endif

EXPORT_SYMBOL float func(int int_param, float float_param);

#ifdef __cplusplus
}
#endif

My first point of confusion is

#ifdef _MSC_VER
    #define EXPORT_SYMBOL __declspec(dllexport)
#else
    #define EXPORT_SYMBOL
#endif

I understand this as "if we are compiling from a windows machine, do something special".

  1. What does __declspec(dllexport) do exactly?
  2. Why do I need EXPORT_SYMBOL at all?

Secondly I am confused about the necessity of wrapping the header in the extern "C" statement. As far as I can tell the header is checking to see if __cplusplus is defined, that is, if we are compiling with g++. The extern block is here to make sure I don't have issues with the g++ compiler if func() was found in say, a file called func.c instead of a .cpp file, and then compiled with gcc. I understand mangling, and how g++ changes function names to enable overloading.

  1. As i have it contained in a .cpp file and hence intend to exclusively compile it with g++, do I need the extern "C" block, and #ifdef statements? Is there a case where this is crucial?
Achnos
  • 83
  • 8
  • 1
    If we are compiling this code with Visual Studio, do Visual Studio-specific things. Otherwise do nothing. – user4581301 Sep 13 '21 at 18:42
  • 1
    Read up on C++ name mangling. Here are 2 good SO questions on the topic: [What is name mangling, and how does it work?](https://stackoverflow.com/questions/1314743/what-is-name-mangling-and-how-does-it-work) and [questions about name mangling in C++](https://stackoverflow.com/questions/2937273/questions-about-name-mangling-in-c) – user4581301 Sep 13 '21 at 18:45
  • 1
    It also ought to be easy to find out exactly what __declspec(dllexport) does, there are plenty of pages about it. – SoronelHaetir Sep 13 '21 at 19:00

1 Answers1

3

What does __declspec(dllexport) do exactly?

When DLL is created, it does not export any symbols by default (unlike static library). In Visual C++, __declspec(dllexport) is the easiest way to export symbols. There are other ways to export.

As i have it contained in a .cpp file and hence intend to exclusively compile it with g++, do I need the extern "C" block, and #ifdef statements? Is there a case where this is crucial?

This allows using the function if you load the DLL from a C program, from a C++ program built by any compiler, or even from other languages with C bindings. If you work exclusively with C++ and g++ compiler, there is still a case when this is important: loading the DLL dynamically - in which case you need to supply your function name as a string (mangled unless you use extern "C").

Eugene
  • 6,194
  • 1
  • 20
  • 31