Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do DLLs have a private section?

Based on what I’ve read about exporting symbols from a DLL in Microsoft’s documentation, you can tell the linker to not include a symbol in the .lib import file by appending the PRIVATE keyword to the export. This, in effect, hides that symbol from application code that uses the library.

My question is, doesn’t the C++ keyword static already make variables/functions invisible to any external translation units?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

static and PRIVATE (wrt. DLLs, similar to -fvisibility=hidden for Linux DSOs) are two different concepts.

The C++ standard as such is agnostic to DLLs and its implementations on different operating systems. static is applied on a translation unit (TU) level, i.e. it restricts the visibility of a symbol (function/variable) to the file is was defined in. So if you have two files a.cpp and b.cpp, neither can see the static symbols of the other. Say you define a function static void foo() in a.cpp. There is no way b.cpp (or any other TU) can see foo(), because you’ve restricted its visibility to the file it was defined in.

On the contrary, PRIVATE restricts visibility on the DLL level, i.e. what is accessible when using the DLL. Meaning that the function void bar() (note: non-static) defined in a.cpp can be used by b.cpp, either by an extern declaration or by including some header a.h. However if you declare bar() as PRIVATE in your .DEF file, users of your DLL won’t be able to call bar(), because the respective symbol won’t be exported.

In that respect, static is a bit more restrictive than PRIVATE, because it already restricts visibility on TU level.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading