Search code examples
c++visual-studioc++-cliopen3d

C++/CLI project, wraper for C++ shared library, get error


I am trying to use Open3d as C++ in managed code C#.

I managed to make a dynamic shared library of open3d as runtime library MD(Multi-threaded DLL). And then add Open3D.lib to the C++/CLI project, but when I compile it I got an LNK2005 error like below:

Error   LNK2005 "public: virtual char const * __cdecl std::exception::what(void)const " 
(?what@exception@std@@UEBAPEBDXZ) already defined in Open3D.lib(Open3D.dll) 
Open3D wrapper test C:\Users\tpgns\source\repos\Open3dWrapperDll\Open3D wrapper test\MSVCRT.lib(throw_bad_alloc.obj)    

As searching through StackOverflow, found a Link which seems the exact solution for my problem, but I can't understand the solution.

  1. Turn off CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS.
  2. Export/import explicitly the desired symbols from the DLL (using __declspec(export|import).

When I turn off CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS in CMakeLists.txt for building Open3D, I can't build the Open3d library. And It seems impossible to add __declspec(export) for every variable inside the Open3d library.

Is there any other solution for my problem? Or is there missing parts in my method to follow the Link solution?


Solution

  • I managed to use Open3d Library in C++/CLI by build open3d as not use static windows runtime library and not build a shared library.

    It works fine when I change CMakeList options as

    option(BUILD_SHARED_LIBS    "Build shared libraries"    OFF)
    option (STATIC_WINDOWS_RUNTIME     "Use static (MT/MTd) Windows runtime"      OFF)
    

    I think It is almost impossible to use the Open3D shared library in C++/CLI.