Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake ignore exact option in findPackage for protobuf

I have a problem with cmake

I'm writting in CMakeLists

set(PROTOBUF_VERSION "2.4.1")
find_package(Protobuf ${PROTOBUF_VERSION} EXACT REQUIRED)

But when I run cmake on my machine with protobuf 2.5.0, it successfully generates makefile.
In stdout I have only:

-- Found PROTOBUF: /usr/local/lib/libprotobuf.so

But for ZLIB I have

-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.7")

Probably, protobuf doesn't contain a version of itself in the library. Is there a way to specify protobufer's version?

like image 899
blackbass1988 Avatar asked Nov 12 '13 03:11

blackbass1988


1 Answers

I'm not sure whether the fault is in protobuf, or in the CMake module, but you have a couple of choices here.

If the find_package call succeeds, you should have access to both the protobuf include path, and to the protoc compiler. You can either read in the contents of ${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h and do a regex search for #define GOOGLE_PROTOBUF_VERSION, or you can invoke protoc --version and search the output for the correct version.

So, for option 1, you can do:

find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED)
if(NOT EXISTS "${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h")
  message(FATAL_ERROR "Failed to find protobuf headers")
endif()

file(STRINGS "${PROTOBUF_INCLUDE_DIRS}/google/protobuf/stubs/common.h" Found
     REGEX "#define GOOGLE_PROTOBUF_VERSION 2004001")
if(NOT Found)
  message(FATAL_ERROR "Didn't find v2.4.1 of protobuf")
endif()

or for option 2:

find_package(Protobuf ${PROTOBUF_VERSION} REQUIRED)
if(NOT PROTOBUF_PROTOC_EXECUTABLE)
  message(FATAL_ERROR "Failed to find protoc")
endif()

execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --version
                OUTPUT_VARIABLE VersionInfo)
string(FIND "${VersionInfo}" "2.4.1" Found)
if(Found LESS 0)
  message(FATAL_ERROR "Didn't find v2.4.1 of protobuf")
endif()
like image 96
Fraser Avatar answered Nov 14 '22 19:11

Fraser