Matthias Lochbrunner Matthias Lochbrunner - 1 year ago 156
C++ Question

`__declspec(dllexport) extern std::string foo;` not found by linker

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_;


I am not that detailed familiar with the keyword
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

const ::std::string* empty_string_;

void InitEmptyString() {
empty_string_ = new string;

See: and following lines.

Does someone know a good workaround for this issue, with that information?

Answer Source

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.hfile (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:

#error defining LIBPROTOBUF_EXPORTS only allowed on DLL creation!

#error defining PROTOBUF_USE_DLLS is required for DLL usage!

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download