protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.37k stars 15.46k forks source link

Compiler: some errors reported from the port_def.h when i try to compile a demo cpp project. #8117

Closed mzygQAQ closed 3 years ago

mzygQAQ commented 3 years ago

Here are the error infos: It seems to be that the port_def.h was included more than once.

g++ src/main.cpp src/user/IM_UserModule.pb.cc -o imServer -I /usr/local/include -L /usr/local/lib -lprotobuf In file included from src/main.cpp:1: In file included from src/user/IM_UserModule.pb.h:22: In file included from /usr/local/include/google/protobuf/arena.h:55: In file included from /usr/local/include/google/protobuf/arena_impl.h:39: In file included from /usr/local/include/google/protobuf/stubs/common.h:46: In file included from /usr/local/include/google/protobuf/stubs/macros.h:34: In file included from /usr/local/include/google/protobuf/stubs/port.h:43: /usr/local/include/google/protobuf/port_def.inc:56:2: error: PROTOBUF_NAMESPACE was previously defined

error PROTOBUF_NAMESPACE was previously defined

^ /usr/local/include/google/protobuf/port_def.inc:59:2: error: PROTOBUF_NAMESPACE_ID was previously defined

error PROTOBUF_NAMESPACE_ID was previously defined

^ /usr/local/include/google/protobuf/port_def.inc:62:2: error: PROTOBUF_ALWAYS_INLINE was previously defined

error PROTOBUF_ALWAYS_INLINE was previously defined

^ /usr/local/include/google/protobuf/port_def.inc:65:2: error: PROTOBUF_COLD was previously defined

error PROTOBUF_COLD was previously defined

^ /usr/local/include/google/protobuf/port_def.inc:68:2: error: PROTOBUF_NOINLINE was previously defined

mzygQAQ commented 3 years ago

// Generated by the protocol buffer compiler. DO NOT EDIT! // source: proto/IM_UserModule.proto

ifndef GOOGLE_PROTOBUF_INCLUDED_proto_2fIM_5fUserModule_2eproto

define GOOGLE_PROTOBUF_INCLUDED_proto_2fIM_5fUserModule_2eproto

include

include

include <google/protobuf/port_def.inc>

if PROTOBUF_VERSION < 3014000

error This file was generated by a newer version of protoc which is

error incompatible with your Protocol Buffer headers. Please update

error your headers.

endif

if 3014000 < PROTOBUF_MIN_PROTOC_VERSION

error This file was generated by an older version of protoc which is

error incompatible with your Protocol Buffer headers. Please

error regenerate this file with a newer version of protoc.

endif

include <google/protobuf/port_undef.inc>

include <google/protobuf/io/coded_stream.h>

include <google/protobuf/arena.h>

include <google/protobuf/arenastring.h>

include <google/protobuf/generated_message_table_driven.h>

include <google/protobuf/generated_message_util.h>

include <google/protobuf/metadata_lite.h>

include <google/protobuf/generated_message_reflection.h>

include <google/protobuf/message.h>

include <google/protobuf/repeated_field.h> // IWYU pragma: export

include <google/protobuf/extension_set.h> // IWYU pragma: export

include <google/protobuf/unknown_field_set.h>

// @@protoc_insertion_point(includes)

include <google/protobuf/port_def.inc>

mzygQAQ commented 3 years ago

look at the code generate by protoc , why the #include <google/protobuf/port_def.inc> in a file twice , this file is not guard by #ifndef xxx or #pagram once

haberman commented 3 years ago

Including port_def.inc multiple times is ok as long as port_def.inc and port_undef.inc are balanced.

In your code sample, they are indeed balanced (port_undef.inc is included before we include port_def.inc again), so this is not a codegen bug.

Instead it looks like a bug in port_undef.inc, that it is not #undefing these symbols.

houzhj58 commented 3 years ago

You just need regenerate the protobuf-autogen files BY protoc --cpp_out=./ your.proto

arizonatribe commented 2 months ago

Including port_def.inc multiple times is ok as long as port_def.inc and port_undef.inc are balanced.

In your code sample, they are indeed balanced (port_undef.inc is included before we include port_def.inc again), so this is not a codegen bug.

Instead it looks like a bug in port_undef.inc, that it is not #undefing these symbols.

This explanation was helpful; thank you.