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

前端 未结 1 450
我寻月下人不归
我寻月下人不归 2021-01-18 06:06

I needed to make some shared libraries in C++ and I used linux as my developer operating system. I know that I need to make symbols visible if I want to load them via

1条回答
  •  隐瞒了意图╮
    2021-01-18 06:30

    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.

    0 讨论(0)
提交回复
热议问题