I ran into the same problem as mentioned here: Protobuf - Refuses to link vs2013 or vs2015
I figured out that these two lines in generated_message_util.h may cause that problem:
__declspec(dllexport) extern const ::std::string* empty_string_;
__declspec(dllexport) extern ProtobufOnceType empty_string_once_init_;
See: https://github.com/google/protobuf/blob/master/src/google/protobuf/generated_message_util.h#L80
I am not that detailed familiar with the keyword extern
but at the end the linker which tries to use that libarary can not find the two definitions of These variables, which are done in generated_message_util.cc.
const ::std::string* empty_string_;
GOOGLE_PROTOBUF_DECLARE_ONCE(empty_string_once_init_);
void InitEmptyString() {
empty_string_ = new string;
...
}
See: https://github.com/google/protobuf/blob/master/src/google/protobuf/generated_message_util.cc#L51 and following lines.
Does someone know a good workaround for this issue, with that information?
Make sure that your compiler flags and defined preprocessor symbols are set correctly.
__declspec(dllexport)
should be set for creation of the DLL, and your code needs to contain the definition. If you want to use the DLL, then you need __declspec(dllimport)
.
See the port.h
file (src/google/protobuf/stubs/port.h) for the definition of LIBPROTOBUF_EXPORT
. It depends on LIBPROTOBUF_EXPORTS
, so if you want to create the DLL, make sure that LIBPROTOBUF_EXPORTS
is defined. If you want to use the DLL, make sure that LIBPROTOBUF_EXPORTS
is not defined.
In order to identify the problem, you can insert the following code in your project:
#ifdef LIBPROTOBUF_EXPORTS
#error defining LIBPROTOBUF_EXPORTS only allowed on DLL creation!
#endif
#ifndef PROTOBUF_USE_DLLS
#error defining PROTOBUF_USE_DLLS is required for DLL usage!
#endif
It will lead to a compile error if your symbols are wrongly defined. Then you still need to fix your project settings until the conditions are satisfied. I can't help you with it, given the current information.
If the conditions don't trigger the error and the problem persists, there might be something else going on, worth looking into it more detailed.