7

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 dlsym/LoadLibrary. So in linux all of my symbols followed this pattern:

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

I used clang with C++11 enabled and I was able to load f in my host program. When I moved to windows I used GCC 4.8.2 with C++11 enabled and that pattern worked on windows machine too with LoadLibrary. (I needed to use C++11 for new attribute syntax). I know that on windows I need to use __declspec(dllexport) to export symbols from shared library. So what now? Is __declspec(dllexport) not required anymore?

Edit:

I found here that those are synonyms (I think) so the question is that is there an [[gnu::attribute]] for __declspec(dllimport) to avoid using macros and ifdefs for specific targets?

4

1 回答 1

4

符号可见性与 - 略有不同,dllexport主要原因是当您.dll在 Windows 下mingw/下编译 a 时cygwin,链接器的默认行为是选项-export-all-symbols- 即默认情况下它将自动导出您的所有内容.dll

您可以通过使用.def文件或将其中一个__declspec((dllexport))__attribute((dllexport))放在任何例程上来更改此行为(即,如果您指定要导出单个符号,则仅导出声明为导出的符号)。如果库中有很多符号,这可以在 dll 加载时显着提高性能。

如果要使用等效C++属性,则使用[[gnu::dllexport]]

所以,是的,dllexport用来防止你.dll出口世界。

以类似的方式,您可以[[gnu:dllimport]]用于导入外部例程。

阅读文档时要小心;它实际上说的是,当您使用该dllexport属性时,它也会触发该visibility:default行为,除非它被覆盖。

于 2014-09-09T13:34:15.433 回答