cmake_minimum_required(VERSION 3.10)
project(MyDistributedApp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 查找并链接ZeroMQ
find_package(ZeroMQ REQUIRED)
target_link_libraries(MyApp PRIVATE ZeroMQ::ZeroMQ)
# 或者使用gRPC
find_package(gRPC REQUIRED)
target_link_libraries(MyApp PRIVATE gRPC::grpc++)
# Protocol Buffers支持
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDS my_proto.proto)
target_link_libraries(MyApp PRIVATE protobuf::libprotobuf)
# 或者使用FlatBuffers
find_package(Flatbuffers REQUIRED)
flatbuffers_generate_headers(MY_FLATBUFFERS_TARGET my_schema.fbs)
# ZooKeeper客户端
find_package(ZooKeeper REQUIRED)
target_link_libraries(MyApp PRIVATE zookeeper_mt)
# 定义节点类型变量
option(BUILD_MASTER_NODE "Build the master node component" ON)
option(BUILD_WORKER_NODE "Build the worker node component" ON)
if(BUILD_MASTER_NODE)
add_executable(master_node master/main.cpp)
target_link_libraries(master_node PRIVATE MyCommonLib)
endif()
if(BUILD_WORKER_NODE)
add_executable(worker_node worker/main.cpp)
target_link_libraries(worker_node PRIVATE MyCommonLib)
endif()
# 检测Linux平台
if(UNIX AND NOT APPLE)
# Linux特定设置
add_compile_options(-pthread)
find_package(Threads REQUIRED)
target_link_libraries(MyApp PRIVATE Threads::Threads)
endif()
# 发布模式优化
if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(-O3 -march=native)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-flto)
endif()
endif()
# 集成Google Test
include(GoogleTest)
find_package(GTest REQUIRED)
add_executable(distributed_tests test/main.cpp test/node_test.cpp)
target_link_libraries(distributed_tests PRIVATE GTest::GTest GTest::Main MyCommonLib)
gtest_discover_tests(distributed_tests)
# 设置安装目标
install(TARGETS MyApp
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
# 安装配置文件
install(DIRECTORY config/ DESTINATION etc/MyApp)
# 生成Dockerfile
configure_file(Dockerfile.in Dockerfile @ONLY)
# 添加构建后容器打包目标
add_custom_target(package_container
COMMAND docker build -t my-distributed-app .
DEPENDS MyApp
COMMENT "Building Docker container"
)
# 调试符号和地址消毒
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-g -fno-omit-frame-pointer)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-fsanitize=address -fsanitize=undefined)
add_link_options(-fsanitize=address -fsanitize=undefined)
endif()
endif()
project-root/
├── CMakeLists.txt # 主配置
├── cmake/
│ ├── FindXXX.cmake # 自定义查找模块
│ └── Config.cmake.in # 配置模板
├── src/
│ ├── CMakeLists.txt # 源代码配置
│ └── ...
└── test/
├── CMakeLists.txt # 测试配置
└── ...
include(FetchContent)
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.9.1
)
FetchContent_MakeAvailable(json)
target_link_libraries(MyApp PRIVATE nlohmann_json::nlohmann_json)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/config.h"
)
target_include_directories(MyApp PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
这些配置技巧可以帮助您构建高效、可维护的Linux分布式应用程序。根据您的具体需求,可以灵活调整和组合这些配置选项。