一个“好看”的CMake管理应该是这样的:

├── CMakeLists.txt
├── main_class.cpp
├── main.cpp
├── model
│   ├── seg_0415.onnx
│   └── seg_0415.trt
├── Parser
│   ├── CMakeLists.txt
│   ├── data_frame_parser.cpp
│   └── data_frame_parser.h
├── RoadSeg
│   ├── CMakeLists.txt
│   ├── road_segmentation.cpp
│   └── road_segmentation.hpp
├── TCPClient
│   ├── CMakeLists.txt
│   ├── tcp_frame_client.cpp
│   └── tcp_frame_client.h
├── Test
│   ├── main_simple.cpp
│   ├── ptr_mat.cpp
│   └── test_main.cpp
└── Utils
    ├── CMakeLists.txt
    ├── logger.cpp
    └── logger.h

具体而言,比如Parser下面的CMakeLists.txt:

find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED)

# Find FFmpeg components
find_package(PkgConfig REQUIRED)
pkg_check_modules(AVCODEC REQUIRED libavcodec)
pkg_check_modules(AVFORMAT REQUIRED libavformat)
pkg_check_modules(SWSCALE REQUIRED libswscale)
pkg_check_modules(AVUTIL REQUIRED libavutil)

include_directories(
    ${OpenCV_INCLUDE_DIRS}
    ${PCL_INCLUDE_DIRS}
    ${AVCODEC_INCLUDE_DIRS}
    ${AVFORMAT_INCLUDE_DIRS}
    ${SWSCALE_INCLUDE_DIRS}
    ${AVUTIL_INCLUDE_DIRS}
    /usr/include
    ${CMAKE_SOURCE_DIR}/Utils
)

# 添加Parser库
add_library(Parser SHARED
    data_frame_parser.cpp
    data_frame_parser.h
)

target_include_directories(Parser PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(Parser
    ${AVCODEC_LIBRARIES}
    ${AVFORMAT_LIBRARIES}
    ${SWSCALE_LIBRARIES}
    ${AVUTIL_LIBRARIES}
    ${OpenCV_LIBS}
    ${PCL_LIBRARIES}
    lz4
    Utils
)

而main函数的CMakeLists.txt就可以很简洁了:

cmake_minimum_required(VERSION 3.10)
project(DataFrameParser)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

option(BUILD_SHARED_LIBS "Build libraries as shared libraries" ON)

# 添加子目录
add_subdirectory(Utils)
add_subdirectory(Parser)
add_subdirectory(TCPClient)
add_subdirectory(AI)
add_subdirectory(RoadSeg)

add_executable(DataFrameParser main_class.cpp)
target_link_libraries(DataFrameParser
    Parser
    TCPClient
    AI4FS
    RoadSeg
)