0

I used a 3rd party script to automatically generate a C++ project. Specifically a .cpp and .h file that I now wish to compile to a .dll with VS2017.

A portion of the generated code (.cpp) is as follows:

const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_gameinformation_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( protodesc_cold ) = {
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, _has_bits_ ),
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, _internal_metadata_ ),
    ~0u, // no _extensions_
    ~0u, // no _oneof_case_
    ~0u, // no _weak_field_map_
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, gamemessage_ ),
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, eacmessagebuffer_ ),
    PROTOBUF_FIELD_OFFSET(::GameInfo::GameInformation, eacmessagelength_ ),
    0,
    1,
    2,
};

This is the only definition/assignment of a value to this variable in the whole project.

When I try to compile my project (Release x64 in VS), this portion of code throws this error:

Error   C2491   'TableStruct_gameinformation_2eproto::offsets': definition of dllimport static data member not allowed

This is the error I am trying to resolve, specifically.

I googled this, and according to the Microsoft docs, I need to get rid of the value assignment, ie, only retain this much:

const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_gameinformation_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( protodesc_cold )

This does not seem like a robust solution to me. This file was automatically generated and I am quite sure those values are important.

After reading another Stack Overflow question, I ca nconfirm that this code (.h) contains a define that switches between dllimport and dllexport (so please don't mark as duplicate):

#if BUILD_GAMEINFORMATION
#define MY_EXPORT_MACRO __declspec( dllexport )
#else
#define MY_EXPORT_MACRO __declspec( dllimport )
#endif

If I have to take the Microsoft solution, does it matter if these values are lost? Is there somewhere else I can put them? Or is there an alternative?

Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
  • https://github.com/protocolbuffers/protobuf/issues/7567 – Hans Passant Oct 27 '20 at 12:04
  • @HansPassant Thanks for the link! I took a look at it, and compared their patch with what I already had in `google/protobuf/parse_context.h` but I do not have the elements they removed. I did add the elements they added, but they were only comments, and this does not seem to have improved my situation – Jessica Chambers Oct 27 '20 at 15:20
  • 1
    I'm sorry that there is no other method besides Microsoft's method, and it is impossible to determine whether those values are important. Static data members cannot specify a definition in the same program in which a dllimport class is defined. Besides, this [link](https://stackoverflow.com/questions/3491990/c-definition-of-dllimport-static-data-member) may help you. – Barrnet Chou Oct 28 '20 at 07:28
  • @BarrnetChou Thanks for the answer, I did delete the definition of this variable and it cleared the error for that one; I had the identical problem with another variable (that is `const`), so I did the same. However, that one now throws this error `'const' object must be initialized if not 'extern'` as well as `definition of dllimport static data member not allowed`. Any idea as to what I can do about that? – Jessica Chambers Oct 28 '20 at 14:05
  • @JessicaChambers This is [the same question](https://stackoverflow.com/questions/64503877/compiler-c2491-error-solution-requires-loss-of-data) you asked a couple of days ago, and my same comment stands. The way the `.h` is written implies that if you build the .lib then you have to define `BUILD_GAMEINFORMATION` in the makefile or the `cl` command line, otherwise you are supposed to just use the definition provided elsewhere in the library, so you cannot change or (re)initialize that. – dxiv Oct 28 '20 at 16:50
  • @dxiv I'm not using the .lib -that line was just a poor copy/paste that doesn't seem to be necessary to the project. I'm trying to compile it with VS (not the cmd line), do you know where I would define `BUILD_GAMEINFORMATION` in this case? – Jessica Chambers Oct 29 '20 at 10:14
  • @JessicaChambers The error indicates that there is a `__declspec(dllimport)` somewhere in the code that you don't show, but it's hard to guess why or where without the full context. – dxiv Oct 29 '20 at 21:23
  • The only occurrence of the phrase `__declspec(dllimport)` is the one shown here in `BUILD_GAMEINFORMATION` – Jessica Chambers Nov 02 '20 at 10:51
  • It seems difficult to judge the cause based on these information. – Barrnet Chou Nov 03 '20 at 02:03
  • @BarrnetChou I can provide any other information you require, these elements just seemed the most relevant to me – Jessica Chambers Nov 04 '20 at 10:26

0 Answers0