自学内容网 自学内容网

ubuntu cmake CPack将第三方库进行打包

有时需要将程序在其他电脑上执行,需要将第三方库同时进行打包,这里以opencv+pcl为例,使用CPack进行打包

工程文件结构

project/
├── CMakeLists.txt        # CMake构建脚本
├── src        # 源文件
├── main.cpp          # 主程序文件
└── copy_libs.sh          # 脚本,用于收集依赖库
└── build          # 编译文件夹
└── libs          # 第三方动态库

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(project)# 工程名

# 工程编译优化
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-all")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wstack-protector")

# 查找OpenCV库
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

# 查找PCL库
find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

# 添加可执行文件
add_executable(project src/main.cpp)

# 链接OpenCV和PCL库
target_link_libraries(project ${OpenCV_LIBS} ${PCL_LIBRARIES})

# 设置RPATH,以便运行时从/opt/install/lib目录中找到依赖库
set(CMAKE_INSTALL_RPATH "/opt/install/lib")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

# 指定安装目录为/opt/install
set(CMAKE_INSTALL_PREFIX "/opt/install")

# 安装可执行文件到/opt/install/bin目录
install(TARGETS project DESTINATION bin)

# 安装libs目录下的动态库到/opt/install/lib目录
install(DIRECTORY ${CMAKE_BINARY_DIR}/libs/ DESTINATION lib)

# 设置CPack变量,打包deb文件
set(CPACK_GENERATOR "ZIP")              # DEB
set(CPACK_PACKAGE_NAME "project")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_CONTACT "your_email@example.com")
set(CPACK_PACKAGE_DESCRIPTION "这是一个包含OpenCV和PCL依赖库的示例项目")
set(CPACK_PACKAGE_MAINTAINER "您的姓名")
# 设置.deb包的安装前缀
set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/install")
include(CPack)

main.cpp

#include <opencv2/opencv.hpp>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <iostream>

int main() {
    // OpenCV示例
    cv::Mat img = cv::Mat::zeros(100, 100, CV_8UC3);
    cv::circle(img, cv::Point(50, 50), 30, cv::Scalar(0, 0, 255), -1);
    cv::imwrite("circle.png", img);
    std::cout << "Saved image using OpenCV" << std::endl;

    // PCL示例
    pcl::PointCloud<pcl::PointXYZ> cloud;
    cloud.width = 5;
    cloud.height = 1;
    cloud.is_dense = false;
    cloud.points.resize(cloud.width * cloud.height);

    for (auto& point : cloud.points) {
        point.x = 1024 * rand() / (RAND_MAX + 1.0f);
        point.y = 1024 * rand() / (RAND_MAX + 1.0f);
        point.z = 1024 * rand() / (RAND_MAX + 1.0f);
    }
    
    pcl::io::savePCDFileASCII("test_pcd.pcd", cloud);
    std::cout << "Saved PCD file using PCL" << std::endl;

    return 0;
}

copy_libs.sh

#!/bin/bash

# 可执行文件路径
EXECUTABLE=project
# 动态库目标目录
DEST_DIR=libs

# 创建libs目录
mkdir -p ${DEST_DIR}

# 使用ldd获取依赖库并复制到libs目录
ldd ${EXECUTABLE} | grep "=>" | awk '{print $3}' | while read lib; do
    if [ -f "$lib" ]; then
        cp -v "$lib" ${DEST_DIR}
    fi
done

编译工程

cd build
cmake ..
make
../copy_libs.sh
make package

在build文件下生成了对应的文件,这里以ZIP为例,将文件拷贝到另一台主机上,主要系统要一致。

export LD_LIBRARY_PATH=/path/install/lib/:$LD_LIBRARY_PATH
cd bin
./project

原文地址:https://blog.csdn.net/OEMT_301/article/details/143745423

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!