1

My goal is to configure cmake file and build my app with protobuf lib.

My attempted steps:

  1. built protobuf in Ubuntu 20.04 followed this section of instruction from protobuf github repo C++ Protobuf - Unix, including copy protoc to /usr/local/bin
  2. configure my CMakeList.txt as below:
include(FindProtobuf)
find_package(Protobuf REQUIRED)
message("${Protobuf_LIBRARIES}")
message("${Protobuf_INCLUDE_DIRS}")
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS XXX.proto XXX.proto)
message("${PROTO_SRCS}")
message("${PROTO_HDRS}")
add_library(proto_msg_lib ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(proto_msg_lib INTERFACE ${Protobuf_LIBRARIES})

when I ran cmake, besides a long list of errors indicating it can not find a bunch of "include files" related to internal protobuf lib. I also saw this warning which bother me a lot:

Protobuf compiler version 21.12 doesn't match library version

The printout for Protobuf_LIBRARIES and Protobuf_INCLUDE_DIRS are

/usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread
/usr/include

My questions are:

  1. why would find_package(Protobuf REQUIRED) look for protobuf lib in this path /usr/lib/x86_64-linux-gnu/libprotobuf.so?
  2. How can I find this libprotobuf.so for the correct version of protobuf I built(v21.12)?
  3. Do I need to include all the source file header from protobuf , or I can just include a libprotobuf.so (or similar thing)?
Dan.Z.Lu
  • 67
  • 7
  • The first line - `include(FindProtobuf)` - is wrong. A `Find*.cmake` script should be included only via `find_package` command, like in your second line. – Tsyvarev Mar 27 '23 at 16:08
  • @Tsyvarev Oh, I see and agree. I tried commented it out but it didn't change or solve anything for my issues though. – Dan.Z.Lu Mar 27 '23 at 16:16

1 Answers1

0

Did you try to use Protobuf_ROOT to specify your local protobuf install ?
ref: https://cmake.org/cmake/help/latest/variable/PackageName_ROOT.html

note: an other way would be to FetchContent() protobuf in your build directly

message(CHECK_START "Fetching Protobuf")
list(APPEND CMAKE_MESSAGE_INDENT "  ")
set(protobuf_BUILD_TESTS OFF)
set(protobuf_BUILD_SHARED_LIBS OFF)
set(protobuf_BUILD_EXPORT OFF)
set(protobuf_MSVC_STATIC_RUNTIME OFF)
FetchContent_Declare(
  protobuf
  GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
  GIT_TAG "v21.12"
  GIT_SUBMODULES ""
  #PATCH_COMMAND git apply --ignore-whitespace ".../protobuf-v21.12.patch"
)
FetchContent_MakeAvailable(protobuf)
list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "fetched")

note: Since 3.24, You can also use OVERRIDE_FIND_PACKAGE
ref: https://cmake.org/cmake/help/latest/module/FetchContent.html#command:fetchcontent_declare

Mizux
  • 8,222
  • 7
  • 32
  • 48
  • I rather use the v21.12 of protobuf than the local installed v3.6.1 (which I uninstalled). In your FetchContent() solution, would it build the protobuf (protoc and protobuf lib)? What do I need to include in my cmake for it to link to protobuf I built source? – Dan.Z.Lu Mar 29 '23 at 15:00
  • protobuf should have alias so this is the same target – Mizux Mar 29 '23 at 20:25