protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Failed to Build Visual Stuido C++ Project with .pb.h and .pb.cc

Open CodeFish711 opened this issue 2 years ago • 7 comments

What version of protobuf and what language are you using? Version: protobuf-3.19.4 Language: C++ 14

What operating system (Linux, Windows, ...) and version? Windows 10 What runtime / compiler are you using (e.g., python version or gcc version) MSVC 19.29.30141.0 CMake 3.22.0 -- Configure BUILD_SHARED_LIBS

What did you do? Steps to reproduce the behavior:

  1. build Release x64 protobuf.sln using Visual Studio 2019

libprotobuf.dll libprotobuf.lib libprotoc.dll libprotoc.lib protoc.exe

  1. Write my test.proto
syntax = "proto3";

package test;

message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;
}

  1. run .\protoc.exe --cpp_out=./ .\test.proto

test.pb.h test.pb.cc

  1. Create Simple Hello_World ConsoleApplication using using Visual Studio 2019 Release x64

add src test.pb.h and test.pb.cc add link lirary libprotobuf.lib and libprotoc.lib add include dir google copy DLL libprotobuf.dll and libprotoc.dll #include "test.pb.h"

  1. Build

What did you expect to see build suceess What did you see instead?

Error LNK2001 unresolved external symbol "class google::protobuf::internal::ExplicitlyConstructed<class std::basic_string<char,struct std::char_traits,class std::allocator > > google::protobuf::internal::fixed_address_empty_string" (?fixed_address_empty_string@internal@protobuf@google@@3V?$ExplicitlyConstructed@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@123@A)

And once I opened "test.pb.h" in vs editor, hundreds of errors occured e.g some macro marked error PROTOBUF_NAMESPACE_OPEN Error (active) E0077 this declaration has no storage class or type specifier

::PROTOBUF_NAMESPACE_ID:: Error (active) E0276 name followed by '::' must be a class or namespace name

Furthermore, if import "google/protobuf/any.proto";

Error LNK2001 unresolved external symbol "struct google::protobuf::AnyDefaultTypeInternal google::protobuf::Any_default_instance" (?Any_default_instance@protobuf@google@@3UAnyDefaultTypeInternal@12@A)

How Can I correctly build? Thanks a lot!!!

CodeFish711 avatar Mar 25 '22 07:03 CodeFish711

Having the same issue, our project builds fine on Linux but it is getting a lot of errors from the protobuf macros on Visual Studio 2019

ralucado avatar May 30 '22 14:05 ralucado

build protobuf x64 with vs /MDd will get rid of this trouble

an-archi avatar Jun 08 '22 14:06 an-archi

changing the "C++ Language Standard" to "ISO C++ 17 Standard (/std:c++17)" resolved the header issues "Error (active) E0077". Other issues can be resolved using the solution mentioned above by @sigaaron.

vasdev8 avatar Jun 21 '22 10:06 vasdev8

same problem, but can build, run without error

harryqk avatar Jul 05 '22 06:07 harryqk

same problem,but my OS is Ubuntu 18.04 And once I opened "test.pb.h" in vs code, hundreds of errors occured e.g some macro marked error PROTOBUF_NAMESPACE_OPEN Error (active) E0077 this declaration has no storage class or type specifier

::PROTOBUF_NAMESPACE_ID:: Error (active) E0276 name followed by '::' must be a class or namespace name How Can I correctly build? Thanks a lot!!!!!!!!!!!!!!!!!!

KuiLeiZS avatar Sep 06 '22 08:09 KuiLeiZS

I am having the same issue. Compiling with Visual Studio 2022. I've tried the suggestions by @vasdev8 and @sigaaron with no luck

nbreen avatar Sep 10 '22 20:09 nbreen

Same issue here in VS 2022. However, when I open the project as a folder (not as solution), it seems likely to work, although, this is not the desired solution for this issue.

imadourabi avatar Sep 26 '22 12:09 imadourabi

  1. I have same issue in vs2019 when i build grpc examples.
  2. I try to enable grpc_MSVC_STATIC_RUNTIME ,protobuf_MSVC_STATIC_RUNTIME,Cares_MSVC_STATIC_RUNTIME in cmake configuration,then I try to generate a static lib by”.grpc.pb.cc“、”.grpc.pb.h“、”.pb.cc“、”.pb.h“rather than dynamic library.
  3. although it still have errors such as E0077,but it build success 。
  4. According to the header file "<google/protobuf/port_def.inc>",The definitions in this file are intended to be portable across Clang,GCC, and MSVC. Function-like macros are usable without an #ifdef guard. Syntax macros (for example, attributes) are always defined, although they may be empty.
  5. maybe just the IntelliSense error?

HMS-Yukikaze avatar Apr 14 '23 02:04 HMS-Yukikaze

Same issue in VS2022.

onlyou2030 avatar Aug 02 '23 08:08 onlyou2030

changing the "C++ Language Standard" to "ISO C++ 17 Standard (/std:c++17)" resolved the header issues "Error (active) E0077". Other issues can be resolved using the solution mentioned above by @sigaaron.

changing standard to c++17 successfully clears errors image

historyliao avatar Aug 08 '23 10:08 historyliao

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar Jan 23 '24 10:01 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Feb 07 '24 10:02 github-actions[bot]