C ++ [[gnu :: visibility ("default")]] vs __declspec (dllexport) on Windows and Linux

I needed to make some common C ++ libraries, and I used linux as my developer operating system. I know that I need to make characters visible if I want to load them through dlsym / LoadLibrary . Thus, on linux, all of my characters follow this pattern:

 extern "C" [[gnu::visibility("default")]] void f(); 

I used clang with C ++ 11 enabled, and I managed to load f in my host program. When I switched to windows, I used GCC 4.8.2 with C ++ 11 enabled, and this template worked on a Windows machine also with LoadLibrary . (I needed to use C ++ 11 for the new attribute syntax). I know that in windows I need to use __declspec(dllexport) to export characters from a shared library. And now what? Is __declspec(dllexport) no longer required?

Edit:

I found here that these are synonyms (I think), so the question is what exists [[gnu::attribute]] for __declspec(dllimport) to avoid using macros and ifdef for specific purposes?

+7
c ++ visibility attributes
source share
1 answer

Character visibility is significantly different from dllexport - and the main reason is that when compiling .dll on Windows under mingw / cygwin , the default linker behavior is the -export-all-symbols option - that is, it will automatically export everything from your .dll to by default.

You can change this behavior either by using the __declspec((dllexport)) file or by placing either __declspec((dllexport)) or __attribute((dllexport)) in any procedure (i.e. if you specify that one character should be exported, then only those characters declared exported are displayed). This can significantly improve performance when loading dlls if your library has a lot of characters.

If you want to use the equivalent C++ attribute, then you use [[gnu::dllexport]]

So use dllexport so that your .dll does not export the world.

Similarly, you can use [[gnu:dllimport]] to import external procedures.

Caution reading the documentation; which actually says that when you use the dllexport attribute, it also triggers the visibility:default behavior if it is not overridden.

+4
source share

All Articles