问题
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