Generate a DLL which uses protocol-buffers

佐手、 提交于 2021-02-05 11:16:07

问题


I am working on a C++ (VS2017) project that uses a protobuf type I created.

However, this project requires a .dll of said protobuf type. The __declspec( dllexport ) in each class declaration are not there by default, and I read online that they can be added by generating the protobuf object with this command line:

--cpp_out=dllexport_decl=MY_EXPORT_MACRO:output/directory

No one has explained what MY_EXPORT_MACRO is or how to define it. When I first generated my protobuf objects I used the most basic line and it worked:

protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/my_file.proto

What and where is MY_EXPORT_MACRO and/or is there another way to make my protobuf files .dll compatible?


回答1:


You know about __declspec( dllimport ) also, correct? What's the easiest way to use the same type definition while building the DLL (with dllexport annotations) and in DLL clients (with dllimport annotations)?

Having a macro to switch the annotation is an extremely common practice in Win32 DLL development of all sorts, not just for protobuf DLLs.

Usually the definition runs like this:

#if BUILD_DLLX
#  define DLLX_API __declspec(dllexport)
#else
#  pragma comment('lib', 'dllx.lib')
#  define DLLX_API __declspec(dllimport)
#endif

And then you would use --cpp_out=dllexport_decl=DLLX_API:$DST_DIR so that the generated header files have DLLX_API inserted in the right places. Then build the DLL with /DBUILD_DLLX so it exports the types and functions.

Consumers of the DLL can #include the exact same header file, and with no /DBUILD_DLLX in their project configuration, they'll end up with imports.



来源:https://stackoverflow.com/questions/64449006/generate-a-dll-which-uses-protocol-buffers

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