就是代码写好了,肯定不能发源码出去,肯定发的是可执行文件,或者就依赖和可执行文件都打包好~

如果是Github管理的话,甚至还要考虑一下Release管理。

因为习惯了CMake,所以直观地想到使用CMakeLists.txt来配置。举例如下:

cmake_minimum_required(VERSION 3.10)
project(MoInfo2GSBag)

set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/../install" CACHE PATH "Install path prefix" FORCE)
option(BUILD_SHARED_LIBS "Build libraries as shared libraries" ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(PCL REQUIRED)
find_package(OpenCV REQUIRED)

include_directories(
    ${OpenCV_INCLUDE_DIRS}
    ${PCL_INCLUDE_DIRS}
)

# add subdirectories
add_subdirectory(AI)
add_subdirectory(RoadSeg)

## -- for MoInfo2GSBag -- ##
find_package(spdlog 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(
    ${AVCODEC_INCLUDE_DIRS}
    ${AVFORMAT_INCLUDE_DIRS}
    ${SWSCALE_INCLUDE_DIRS}
    ${AVUTIL_INCLUDE_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/include
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/proto
    /usr/include
)

link_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/external/platform_sdk/lib
    )

add_library(metoak_proto_gsbag
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/proto/metoak_image_msg.pb.cc 
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/proto/metoak_image_msg.pb.h
)

target_link_libraries(metoak_proto_gsbag
    ${PROTOBUF_LIBRARIES}  
)

add_executable(MoInfo2GSBag moinfo_to_gsbag.cpp)
target_link_libraries(MoInfo2GSBag
    spdlog::spdlog
    ${AVCODEC_LIBRARIES}
    ${AVFORMAT_LIBRARIES}
    ${SWSCALE_LIBRARIES}
    ${AVUTIL_LIBRARIES}
    ${OpenCV_LIBS}
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libsqlcipher.so
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libgsbag.so
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libcommon.so
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libgaclog.so
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libgacbag_storage.so
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libgacbag_cpp.so
    ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/libgacbag_serializer.so
    protobuf
    metoak_proto_gsbag
    lz4
    AI4FS
    RoadSeg
)


## -- Install -- ##
install(TARGETS MoInfo2GSBag DESTINATION bin)
install(TARGETS 
    metoak_proto_gsbag 
    AI4FS
    RoadSeg
    DESTINATION lib)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/ DESTINATION lib
        FILES_MATCHING PATTERN "*.so*")

# CPack configuration
set(CPACK_GENERATOR "TGZ;DEB")
set(CPACK_PACKAGE_NAME "MoInfo2GSBag")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
set(CPACK_PACKAGE_CONTACT "ChangKunYang <changkun.yang@metoak.net>")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libavcodec, libavformat, libswscale, libavutil, protobuf, spdlog")
set(CPACK_PACKAGE_DESCRIPTION "MoInfo2GSBag tool with all dependencies included")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A tool to process MoInfo2GSBag data with all required libraries included")
include(CPack)

其中的set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/../install" CACHE PATH "Install path prefix" FORCE)就是用来配置install的位置的,如果没有设置,默认是/usr/local,这时候使用cmake install是需要sudo权限的。

其次,还需要注意安装的lib要全!

注意这种写法:

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/MoBag/lib/ DESTINATION lib
        FILES_MATCHING PATTERN "*.so*")

将所有的文件都移过来,包括软链接的文件。

然后通过cmake install就可以了,实际使用时再临时链接一下动态库,比如:

# 在目标机器上
cd /path/to/destination/install

# 设置 LD_LIBRARY_PATH 环境变量,以便找到所需的库
export LD_LIBRARY_PATH=$(pwd)/lib:$LD_LIBRARY_PATH

# 运行可执行文件
./bin/MoInfo2GSBag

CPack

其实install已经够用了,更高级的操作的是使用CPack进行打包管理,就是直接cpack指令就能实现。

特别注意一下,所有的CPack变量需要在include(CPack)之前设置,这样CPack能够正确读取这些变量。

CPack的配置说明

# CPack configuration
set(CPACK_GENERATOR "TGZ;DEB")
set(CPACK_PACKAGE_NAME "MoInfo2GSBag")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
set(CPACK_PACKAGE_CONTACT "ChangKunYang <changkun.yang@metoak.net>")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libavcodec, libavformat, libswscale, libavutil, protobuf, spdlog")
set(CPACK_PACKAGE_DESCRIPTION "MoInfo2GSBag tool with all dependencies included")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A tool to process MoInfo2GSBag data with all required libraries included")
include(CPack)