cmake find_package具有库protobuf的指定位置

时间:2018-04-10 18:58:13

标签: c++ cmake protocol-buffers

我的项目需要protobuf 3.5.1,我构建它并希望cmake使用它而不是安装在系统(2.5.0)中。这是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

project(test)

set(ENV{PKG_CONFIG_PATH} "/path_to_protobuf/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

find_package(Protobuf 3.5.1 REQUIRED)

link_directories(
        ${PROTOBUF_LIBRARY_DIRS}
)

include_directories(
        src
        ${PROTOBUF_INCLUDE_DIRS}

)

protobuf_generate_cpp(
        PROTO_SRCS PROTO_HDRS
        ../my_proto.proto
)

set(sourceDir "${PROJECT_SOURCE_DIR}/src")
file(GLOB_RECURSE allFiles "${sourceDir}/*.h" "${sourceDir}/*.cpp")

add_executable(
        test
        ${allFiles}
        ${PROTO_SRCS}
        ${PROTO_HDRS})

target_link_libraries(
        test
        ${PROTOBUF_LIBRARIES}
)

但是cmake完成了错误消息:

CMake Warning at lib_build/share/cmake-3.11/Modules/FindProtobuf.cmake:455 (message):
  Protobuf compiler version 3.5.1 doesn't match library version 2.5.0
Call Stack (most recent call first):
  CMakeLists.txt:36 (find_package)


CMake Error at lib_build/share/cmake-3.11/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find Protobuf: Found unsuitable version "2.5.0", but required is
  at least "3.5.1" (found /usr/lib64/libprotobuf.so;-lpthread)
Call Stack (most recent call first):
  lib_build/share/cmake-3.11/Modules/FindPackageHandleStandardArgs.cmake:376 (_FPHSA_FAILURE_MESSAGE)
  lib_build/share/cmake-3.11/Modules/FindProtobuf.cmake:543 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:36 (find_package)


— Configuring incomplete, errors occurred!
See also "/home/user/app/CMakeFiles/CMakeOutput.log".
See also "/home/user/app/CMakeFiles/CMakeError.log".

如何正确指定模块搜索路径? 如果不编写自己的FindProtobuf.cmake文件,我可以这样做吗?

0 个答案:

没有答案