Skip to content

Instantly share code, notes, and snippets.

@kfsone
Created September 4, 2025 23:09
Show Gist options
  • Save kfsone/f375bd1cb001433b2bdd84f342e79c72 to your computer and use it in GitHub Desktop.
Save kfsone/f375bd1cb001433b2bdd84f342e79c72 to your computer and use it in GitHub Desktop.
cmake_minimum_required (VERSION 3.25..3.31)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
project (Example LANGUAGES CXX)
include (FetchContent)
FetchContent_Declare (
protobuf
GITHUB_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GITHUB_TAG main
SOURCE_SUBDIR cmake
FIND_PACKAGE_ARGS NAME protobuf
)
FetchContent_MakeAvailable (
protobuf
)
include (FindProtobuf)
find_package (Protobuf REQUIRED) # <- not possible, it hasn't been built
add_target (MyBufs)
target_include_directories (MyBufs PUBLIC ${CMAKE_BINARY_DIR})
target_include_directories (MyBufs PUBLIC ${Protobuf_INCLUDE_DIRS})
target_link_libraries (MyBufs PUBLIC ${Protobuf_LIBRARIES})
# Collect a list of the .proto files in this directory
file (GLOB _protos *.proto)
# Tell cmake that we need to use those with protoc to generate the .cpp/.h files
PROTOBUF_GENERATE_CPP (
_sources _headers
${_protos}
)
# Add those as sources to the library (it won't try to compile the headers,
# but it ought to notice if they change for rebuild purposes)
target_sources (
MyBuffs
${_sources}
${_headers}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment