4

The following is a common pattern in cross-platform libraries that are compiled as shared libraries with default visibility hidden:

#if defined _WIN32
  #ifdef BUILDING_DLL
    #define DLL_PUBLIC __declspec(dllexport)
  #else
    #define DLL_PUBLIC __declspec(dllimport)
  #endif
#else
  #define DLL_PUBLIC __attribute__ ((visibility ("default")))
#endif

How is it that on Visual Studio we need two different keywords when compiling and when using the shared library, while with the GNU compiler we get away with only one?

Theories:

  • Are the methods not as equivalent as they seem and therefore are there pitfalls to watch out for?
  • Is the two keyword method on Visual Studio there only for historic reasons?
  • Are there some internal technical differences that make the keyword necessary with Visual Studio (for example the way information is passed to the linker)?
mastov
  • 2,942
  • 1
  • 16
  • 33
  • *How is it that on Windows* -- You are comparing apples to oranges. It is the Microsoft Visual C++ compiler where those attributes/keywords are used, not "Windows". GCC also runs on Windows. – PaulMcKenzie Aug 15 '23 at 12:11
  • 1
    @PaulMcKenzie Corrected the phrasing. Even though I think it was obvious from the phrasing that I was referring to "Visual Studio". – mastov Aug 15 '23 at 12:17
  • 1
    Also [__declspec](https://stackoverflow.com/questions/2284610/what-is-declspec-and-when-do-i-need-to-use-it) is used for more than importing and exporting symbols. Microsoft decided to use the `__declspec` keyword for this purpose. – PaulMcKenzie Aug 15 '23 at 12:23
  • 1
    The `__declspec(dllimport)` and `__declspec(dllexport)` has been around for my entire 26 years I have worked professionally on windows systems programming with msvc. So I would say this does have a bit if history behind it. – drescherjm Aug 15 '23 at 12:25
  • Interestingly, MinGW both supports `__declspec(dll{im,ex}port)` *and* can work without it. – HolyBlackCat Aug 15 '23 at 12:37
  • @PaulMcKenzie And the fact that they use "__declspec" prevents them from using a single keyword like "__declspec(dlldefaultvisibility)"? – mastov Aug 15 '23 at 12:39
  • I am not an engineer working for Microsoft. They have their reasons. If you want to know why, Microsoft would be the ones to ask. – PaulMcKenzie Aug 15 '23 at 12:42
  • @PaulMcKenzie OK, sorry, I just assumed the comment was related to the question. – mastov Aug 15 '23 at 12:45
  • 1
    @drescherjm -- `__declspec` has been around for at least 30 years. – PaulMcKenzie Aug 15 '23 at 12:51
  • Thanks! Before I started my current job on May 19, 1997, most of my development was done in Turbo Pascal with some Turbo C. I don't remember much of either other than the dos based IDEs. At work I started with Microsoft Developer Studio 4.X and was writing c code at the start. Then MFC and since 2008 Qt. – drescherjm Aug 15 '23 at 13:02
  • This is a [duplicate](https://stackoverflow.com/questions/58846822/why-does-msvc-visual-c-need-separate-dllimport-and-dllexport-attributes-and?rq=2), although the answer there covers only half the question. – Davis Herring Aug 18 '23 at 13:34

0 Answers0