Dan.Z.Lu
Dan.Z.Lu

Reputation: 77

Cmake find_package(Protobuf REQUIRED) does not work out as I wanted

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)?

Upvotes: 2

Views: 2084

Answers (1)

Mizux
Mizux

Reputation: 9329

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

Upvotes: 0

Related Questions