Irbis
Irbis

Reputation: 1491

symbols visibility - shared library vs dll

I have a C++ dynamic library which defines global symbols - functions. When I compile the library using gcc those symbols are by default visible. It may leads to symbol collisions when an application links to a few libraries. I can put functions in the namespace and hide private functions using the compilation flag -fvisibility=hidden. Then API functions should contain the following attribute: __attribute__ ((visibility ("default")))

Let's assume that I compile a dynamic library on Windows using Microsoft Visual Studio compiler. Some symbols are not defined with __declspec(dllexport) Does it mean that those symbols will be hidden ?

Upvotes: 2

Views: 1478

Answers (1)

yugr
yugr

Reputation: 21886

Both POSIX and Windows provide different means to control symbol visibility. Source-code annotations (__attribute__((visibility("default"))) and __declspec(dllexport)) are the most common way to do this. Windows __declspec(dllexport) is indeed absolutely equivalent to POSIX -fvisibility=hidden + __attribute__((visibility("default"))) in this respect.

But both platforms also provide other alternatives to set visibility e.g. export symbol files and version scripts on POSIX and DEF files on Windows. These alternatives are less common (mainly because they are less portable and prevent some important optimizations).

So whether __declspec is "enough" and in what sense entirely depends on your project's buildscripts. If DEF files and linker scripts are not used - all symbols not explicitly marked with __declspec(dllexport) will be hidden.

Upvotes: 6

Related Questions