protocolbuffers / protocolbuffers/protobuf
How to import proto files between different files using cmake
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 72k
- Forks
- 16.3k
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 140
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with msg_b/CMakeLists.txt and msg_b/addressbook.proto, then compare the import path and generated-file setup with msg_a/CMakeLists.txt. Reproduce the reported build and determine what configuration is needed for the cross-directory proto import to compile without the descriptor-table error; no specific test is mentioned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100