protobuf
protobuf copied to clipboard
Failed to Build Visual Stuido C++ Project with .pb.h and .pb.cc
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:
- build Release x64 protobuf.sln using Visual Studio 2019
libprotobuf.dll libprotobuf.lib libprotoc.dll libprotoc.lib protoc.exe
- Write my test.proto
syntax = "proto3";
package test;
message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
}
- run
.\protoc.exe --cpp_out=./ .\test.proto
test.pb.h test.pb.cc
- Create Simple Hello_World ConsoleApplication using using Visual Studio 2019 Release x64
add src
test.pb.h
andtest.pb.cc
add link lirarylibprotobuf.lib
andlibprotoc.lib
add include dirlibprotobuf.dll
andlibprotoc.dll
#include "test.pb.h"
- 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!!!
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
build protobuf x64 with vs /MDd will get rid of this trouble
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.
same problem, but can build, run without error
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!!!!!!!!!!!!!!!!!!
I am having the same issue. Compiling with Visual Studio 2022. I've tried the suggestions by @vasdev8 and @sigaaron with no luck
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.
- I have same issue in vs2019 when i build grpc examples.
- 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.
- although it still have errors such as E0077,but it build success 。
- 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.
- maybe just the IntelliSense error?
Same issue in VS2022.
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
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.
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.