平常代码开发中,有时候需要将写的程序打包成压缩包放到目标设备上进行运行测试。用CMake管理工程,实现使用
make -jnproc package
指令可以将工程进行打包,可执行文件存储在bin
文件夹中,依赖库存储在lib
文件夹中。
示例
- 1.工程目录结构
- CMakeLists.txt
- License.txt
- src
- include
- 2.打包后
1.工程目录结构
package_test/
│
├── CMakeLists.txt
│
├── License.txt
│
├── src/
│ ├── main.cpp
│ └── lib.cpp
│
└── include/
└── lib.h
CMakeLists.txt
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
project(package_test VERSION 1.0)
message("VERSION 1.0.0")
# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_compile_options("-Wno-unused-variable")
# 指定可执行文件和库文件的输出目录
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
endif()
# 添加库
add_library(mylib SHARED src/lib.cpp)
target_include_directories(mylib PUBLIC include)
# 添加可执行文件
add_executable(${PROJECT_NAME} src/main.cpp)
# 链接库到可执行文件
target_link_libraries(${PROJECT_NAME} PRIVATE mylib)
# 设置RPATH(运行时库搜索路径)
set_target_properties(${PROJECT_NAME} PROPERTIES
OUTPUT_NAME ${PROJECT_NAME}
INSTALL_RPATH "\${ORIGIN}/../lib"
)
# 安装规则
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
install(TARGETS mylib DESTINATION lib)
# 包括 CPack 打包配置
include(InstallRequiredSystemLibraries)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${package_test_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${package_test_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${package_test_VERSION_PATCH}")
set(CPACK_GENERATOR "ZIP")
set(CPACK_PACKAGE_NAME "package_test")
set(CPACK_PACKAGE_FILE_NAME package_test-${package_test_VERSION})
include(CPack)
endif()
License.txt
MIT License
Copyright (c) 2024 boss_dog
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
src
main.cpp
#include <iostream>
#include "../include/lib.h"
int main()
{
std::cout << "Hello from Main!" << std::endl;
libFunction();
return 0;
}
lib.cpp
// lib.cpp
#include "../include/lib.h"
#include <iostream>
void libFunction()
{
std::cout << "Hello from Library!" << std::endl;
}
include
lib.h
// lib.h
#ifndef LIB_H
#define LIB_H
void libFunction();
#endif
2.打包后
在工程文件夹下执行如下命令:
mkdir build
cd build
cmake ..
make -j`nproc` package
会生成package_test-1.0.zip
压缩包