c++ - protocol buffers - generate NON-inline accessors -


we're using protocol buffers (2.4.1) in medium size embedded system w/ c# c++ code. use protobufs isolate our managed , native layers w/ easy maintain serialization layer (for curious, have used pinvoke, have run native code in separate process on test/simulators).

our system has number of dll's, , have generated native protobuf code in it's own dll other parts of system can don't have link in generated code directly.

the problem i'm having generated accessors inline, eg:

inline const ::myprotoclassname::myfield& myprotoclassname::myfield() const  {     return myfield_ != null ? *myfield_ : *default_instance_->myfield_;  } 

use generated api in size (the 'default_instance_' dereferenced , accessed if particular field not set). means can't link (lnk2001) clients using accessors because there not symbol default_instance_

i think typical use case protobufs have each component link in generated protobuf code (after all, serialization layer distributed systems),

i'm wondering if there compile switch change inlining behavior missed. (have accessors defined in cc file, not h)

thanks!


  • thanks @g-makulik! looks answer 30 lines in protoc code, didn't see :)

    • < see answer below bulk of solution > should you, though.

as noted in of kenton's changelogs, adding causes several warnings (c4251, c4275) relating base classes not dllexport'd

with way protobufs implemented, , protobuf classes being templates, these warnings benign. cleanly ignore them (eg without having disable warnings clients) used hacky approach:

-wrapper protobuf.h file eveyone includes. (no 1 include real generated h file)

    #pragma once     #pragma warning(push)       #pragma warning(disable:4251)       #pragma warning(disable:4275)       // include protobuf generated code; exclude warn c4251, c4275       // these relate dll exported          #include "yourprotofile.h"       #pragma warning(pop)   

and wrapper c file (the real protobuf cc file not in project -> not built directly)

#include "myprotofile_wrapper.h"   #include "myprotofile.cc" 

i think link answer question: protobuf msvc: how export generated message

quote kenon varga (project leader of google protobuf):

if invoke protoc like:

protoc --cpp_out=dllexport_decl=my_export_macro:path/to/output/dir myproto.proto

then generate code my_export_macro in right places. however, option incomplete -- there no way force generated .pb.h #include header defines my_export_macro. i'm open patches fix this. or, use hack work around it, such adding #include via sort of text processing after protoc finishes, or perhaps moving .pb.h .pb2.h , replacing .pb.h file 1 first includes header includes .pb2.h...

and additonally cover mentioned incompleteness (quote aron bierbaum):

we around limitation forcing the header included using compiler command line flags:

windows: /fiproject/config.h
linux: -include project/config.h

note:
using inline keyword shouldn't relevant in case (that additional __declspec dllexport/__declspec dllimport attribute put on accessor method). per definition it's compiler inline functions or not. of course seeing __declspec dllexport or __declspec dllimportattribute contradictionary inlining.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -