protocolbuffers/protobuf

How to import proto files between different files using cmake

Open

#9,643 opened on Mar 17, 2022

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C++ (16,128 forks)batch import
c++help wanted

Repository metrics

Stars
 (71,223 stars)
PR merge metrics
 (Avg merge 2d 11h) (185 merged PRs in 30d)

Description

What language does this apply to? If it's a proto syntax change, is it for proto2 or proto3? If it's about generated code change, what programming language?

Describe the problem you are trying to solve. import proto files from different files Describe the solution you'd like

Describe alternatives you've considered

Additional context Add any other context or screenshots about the feature request here. My project file structure and CMakeLists.txt in file msg_b shown below,: . ├── Protocol-Buffer-Examples │   ├── build │   ├── CMakeLists.txt │   ├── docker │   │   └── protobuf.Dockerfile │   ├── examples │   │   ├── add_people │   │   │   ├── add_people.cc │   │   │   └── CMakeLists.txt │   │   ├── CMakeLists.txt │   │   └── list_people │   │   ├── CMakeLists.txt │   │   └── list_people.cc │   ├── LICENSE.md │   ├── msg_a │   │   ├── a.proto │   │   └── CMakeLists.txt │   ├── msg_b │   │   ├── addressbook.proto │   │   ├── CMakeLists.txt │   │   └── CMakeLists.txt.bak │   ├── README.md

And the CMakeLists.txt in file msg_b is : cmake_minimum_required(VERSION 3.14.7)

project(addressbook) message("---------------in msg_b----------------------------:") set(CMAKE_BUILD_TYPE RelWithDebInfo)

Find required protobuf package

find_package(Protobuf REQUIRED) set(PROTOBUF_IMPORT_DIRS)

set(PROTOBUF_IMPORT_DIRS ${CMAKE_SOURCE_DIR}/msg_a)

set(PROTOBUF_IMPORT_DIRS ${project_top_dir}) message("PROTOBUF_IMPORT_DIRS:" ${PROTOBUF_IMPORT_DIRS}) include_directories(${CMAKE_BINARY_DIR}) link_libraries(${CMAKE_BINARY_DIR}/msg_a/liba.so) include_directories(${CMAKE_BINARY_DIR}/msg_a) include_directories(${CMAKE_BINARY_DIR}/msg_b)

file(GLOB PROTO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.proto) foreach(proto ${PROTO_FILES}) # Get filename without extension get_filename_component(PROTO_NAME_WE ${proto} NAME_WE) message("PROTO_NAME_WE:" ${PROTO_NAME_WE}) get_filename_component(PROTO_NAME ${proto} NAME) message("PROTO_NAME:" ${PROTO_NAME}) add_library(${PROTO_NAME_WE} SHARED ${proto}) protobuf_generate(TARGET ${PROTO_NAME_WE} IMPORT_DIRS ${PROTOBUF_IMPORT_DIRS}) target_link_libraries(${PROTO_NAME_WE} PUBLIC ${Protobuf_LIBRARIES} a pthread) target_include_directories(${PROTO_NAME_WE} PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_BINARY_DIR}/msg_a) endforeach()


In the addressbook.proto I use the import "msg_a/a.proto" to import the a.proto, But when i make ,there was an error:

XXXXX/build/msg_b/addressbook.pb.cc:136:6: error: ‘::descriptor_table_msg_5fa_2fa_2eproto’ has not been declared &::descriptor_table_msg_5fa_2fa_2eproto, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ XXXXX/build/msg_b/addressbook.pb.cc:136:6: note: suggested alternative: ‘descriptor_table_a_2eproto’ &::descriptor_table_msg_5fa_2fa_2eproto, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ descriptor_table_a_2eproto

Contributor guide