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

泄露秘密 提交于 2019-12-01 03:42:15

Symbol visibility is subtly different from dllexport - and the primary reason is that when you compile a .dll in Windows under mingw/cygwin, the default behaviour of the linker is the option -export-all-symbols - i.e. it will auto-export everything from your .dll by default.

You can change this behaviour by either using a .def file or putting either __declspec((dllexport)) or __attribute((dllexport)) on any routine (i.e. if you specify that a single symbol is to be exported then only the symbols that are declared exported are exported). This can have a significant performance improvement at dll load time if there are a lot of symbols in your library.

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

So yes, use dllexport to keep your .dll from exporting the world.

In a similar manner you can use [[gnu:dllimport]] for importing external routines.

Careful while reading the documentation; what it actually says is that when you use the dllexport attribute, it also triggers the visibility:default behaviour unless it's overridden.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!