4

How do I properly export shared library symbols (for linking from client code in Visual Studio) while making sure the code is cross-platform? Specifically is using .def the recommanded way or should I use platform-specific macro for __declspec(dllexport)? If macro should be used, can you give an example that is cross platform friendly?

I have some native C++ code for cross platform use, and used Cmake to generate .sln/.vcxproj file which creates .dll file for use in Windows. To link to this dll from a C++/CLI wrapper (also a dll) I need to have .lib, which requires exporting symbols. I learned I need to use either __declspec(dllexport) or .def file. My concern is I don't want to temper the shared code with MSVC stuff (we need to support Linux, iOS, Mac OSX, Android ...).

M W
  • 1,269
  • 5
  • 21
  • 31
  • 3
    Have you seen http://gcc.gnu.org/wiki/Visibility – Seth Carnegie Jul 17 '12 at 16:47
  • You have already answered the question. You can use a .def file to define the exports (without affection the source code), or you can add annotations to the source if that is not important. Your choice! – Bo Persson Jul 17 '12 at 18:21
  • I am not sure if .def is the right approach in this case. I can export function. For exporting class, I need to use mangled names which has its concern: http://stackoverflow.com/questions/186232/exporting-dll-c-class-question-about-def-file – M W Jul 17 '12 at 19:27
  • @SethCarnegie Thank! The link does give me more thoughts about this symbol export/import topic. – M W Jul 17 '12 at 20:11

1 Answers1

3

SethCarnegie's link gcc.gnu.org/wiki/Visibility is probably an indirect answer to this question. In short, I decided to use macro (preprocessor symbols), which seems it would work well for cross platform with some planning. While .def is separated from the code, there is no corresponding way for importing i.e. I still need to use __declspec(dllimport) -- not very clean IMO.

If you are new to this topic (cross platform, using unmanaged C++ from managed code) like me, you can see what I did as an example here (see the question portion) : Link error linking from managed to unmanaged C++ despite linking to .lib file with exported symbols

Community
  • 1
  • 1
M W
  • 1,269
  • 5
  • 21
  • 31