配置protobuf头文件和库
findprotobuf.cmake
MACRO(FIND_PROTOBUF_INCLUDE THIS_PROTOBUF_INCLUDE_DIR THIS_PROTOBUF_INCLUDE_FILE)
FIND_PATH( ${THIS_PROTOBUF_INCLUDE_DIR} ${THIS_PROTOBUF_INCLUDE_FILE} )
ENDMACRO(FIND_PROTOBUF_INCLUDE THIS_PROTOBUF_INCLUDE_DIR THIS_PROTOBUF_INCLUDE_FILE)
FIND_PROTOBUF_INCLUDE(PROTOBUF_INCLUDE_DIR protobuf.h)
MACRO(FIND_PROTOBUF_LIBRARY THIS_LIBRARY THIS_LIBRARY_NAME)
FIND_LIBRARY( ${THIS_LIBRARY} ${THIS_LIBRARY_NAME} )
ENDMACRO(FIND_PROTOBUF_LIBRARY THIS_LIBRARY THIS_LIBRARY_NAME)
FIND_PROTOBUF_LIBRARY(PROTOBUF_LIBRARY protobuf)
FIND_PROTOBUF_LIBRARY(PROTOBUF_LIBRARY_DEBUG protobuf)
FIND_PROTOBUF_LIBRARY(PROTOBUF_LITE_LIBRARY protobuf_lite)
FIND_PROTOBUF_LIBRARY(PROTOBUF_LITE_LIBRARY_DEBUG protobuf_lite)
FIND_PROTOBUF_LIBRARY(PROTOC_LIBRARY protoc)
FIND_PROTOBUF_LIBRARY(PROTOC_LIBRARY_DEBUG protoc)
# 为了下游可以继续使用
set(protobuf_FOUND FALSE)
if (PROTOBUF_INCLUDE_DIR AND PROTOBUF_LIBRARY)
set(protobuf_FOUND TRUE)
endif()
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
MACRO(LINK_WITH_VARIABLES TARGETNAME)
FOREACH(varname ${ARGN})
#release test.lib debug testd.lib
TARGET_LINK_LIBRARIES(${TARGETNAME} optimized "${${varname}}" debug ${${varname}_DEBUG} )
ENDFOREACH(varname)
ENDMACRO()
PROJECT(protobuf_test)
add_executable(${PROJECT_NAME} main.cpp)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
FIND_PACKAGE(protobuf REQUIRED)
IF (protobuf_FOUND)
MESSAGE("protobuf FOUND")
INCLUDE_DIRECTORIES( ${PROTOBUF_INCLUDE_DIR})
LINK_WITH_VARIABLES(protobuf_test PROTOBUF_LIBRARY PROTOBUF_LITE_LIBRARY PROTOC_LIBRARY )
ENDIF(protobuf_FOUND)
配置条目