protobuf-c.{c,h}: use static empty_string on WIN32

MSVC has issues importing protobuf_c_empty_string from the DLL and using
it in initializers, resulting in errors like when linking against
protobuf-c.dll:
  test-proto3.pb-c.c(13): error C2099: initializer is not a constant
  test-proto3.pb-c.c(19): error C2440: 'initializing': cannot convert from 'void *' to 'Foo__Person__PhoneType'

Work around the issue by not exporting protobuf_c_empty string and
defining it as static const char[] in the protobuf-c.h file itself. This
change only applies to _WIN32 shared library builds.
This commit is contained in:
Ilya Lipnitskiy 2021-03-13 10:12:20 -08:00
parent d9f098e3ab
commit 18ede005af
No known key found for this signature in database
GPG Key ID: 435C02AAE7CF2014
2 changed files with 6 additions and 0 deletions

View File

@ -84,7 +84,9 @@
# define PROTOBUF_C_UNPACK_ERROR(...)
#endif
#if !defined(_WIN32) || !defined(PROTOBUF_C_USE_SHARED_LIB)
const char protobuf_c_empty_string[] = "";
#endif
/**
* Internal `ProtobufCMessage` manipulation macro.

View File

@ -238,7 +238,11 @@ PROTOBUF_C__BEGIN_DECLS
#define PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC 0x114315af
/* Empty string used for initializers */
#if defined(_WIN32) && defined(PROTOBUF_C_USE_SHARED_LIB)
static const char protobuf_c_empty_string[] = "";
#else
extern const char protobuf_c_empty_string[];
#endif
/**
* \defgroup api Public API