添加package配置。

This commit is contained in:
2026-03-24 08:57:02 +08:00
parent e13b3650e9
commit df9ad3fc4e
4 changed files with 159 additions and 5 deletions

4
.gitignore vendored
View File

@@ -2,4 +2,6 @@ build/
.cache/
.qtcreator/
.vs
out/
out/
debug/
release/

80
Build.bat Normal file
View File

@@ -0,0 +1,80 @@
@echo off
setlocal enabledelayedexpansion
:: Configuration
set INSTALL_PREFIX=C:/local/cxxLibrary
set BUILD_DEBUG_DIR=debug
set BUILD_RELEASE_DIR=release
set CMAKE_GENERATOR="MinGW Makefiles"
echo ========================================
echo cxxLibrary Build and Install Script
echo ========================================
echo.
:: Check CMake availability
where cmake >nul 2>nul
if %ERRORLEVEL% neq 0 (
echo [ERROR] CMake not found in PATH!
echo Please ensure CMake is installed and added to system PATH.
pause
exit /b 1
)
:: Check MinGW availability
where mingw32-make >nul 2>nul
if %ERRORLEVEL% neq 0 (
echo [WARNING] mingw32-make not found in PATH!
echo This may cause build failures if using MinGW generator.
echo.
)
:: Build Debug configuration
echo [1/4] Configuring Debug build...
cmake -B%BUILD_DEBUG_DIR% -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=%INSTALL_PREFIX%
if %ERRORLEVEL% neq 0 (
echo [ERROR] Debug configuration failed!
pause
exit /b 1
)
echo [2/4] Building and installing Debug version...
cmake --build %BUILD_DEBUG_DIR% --target install
if %ERRORLEVEL% neq 0 (
echo [ERROR] Debug build/install failed!
pause
exit /b 1
)
:: Build Release configuration
echo [3/4] Configuring Release build...
cmake -B%BUILD_RELEASE_DIR% -G %CMAKE_GENERATOR% -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%INSTALL_PREFIX%
if %ERRORLEVEL% neq 0 (
echo [ERROR] Release configuration failed!
pause
exit /b 1
)
echo [4/4] Building and installing Release version...
cmake --build %BUILD_RELEASE_DIR% --target install
if %ERRORLEVEL% neq 0 (
echo [ERROR] Release build/install failed!
pause
exit /b 1
)
:: Verification
echo.
echo ========================================
echo Build completed successfully!
echo ========================================
echo Library installed to: %INSTALL_PREFIX%
echo Debug build directory: %BUILD_DEBUG_DIR%
echo Release build directory: %BUILD_RELEASE_DIR%
echo.
echo To use this library in other projects, add to CMake:
echo list(APPEND CMAKE_PREFIX_PATH "%INSTALL_PREFIX%")
echo find_package(cxxLibrary REQUIRED)
echo target_link_libraries(your_target PRIVATE cxxLibrary::cxxLibrary)
echo ========================================
pause

View File

@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(cxxLibrary LANGUAGES CXX)
project(cxxLibrary VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(SOURCES
@@ -14,18 +14,74 @@ file(GLOB_RECURSE BOOST_SOURCES
)
add_library(cxxLibrary STATIC ${SOURCES} ${BOOST_SOURCES})
target_include_directories(cxxLibrary PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
# 修正1:使用相对路径,避免绝对路径警告
target_include_directories(cxxLibrary PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
target_link_libraries(cxxLibrary PUBLIC ws2_32 bcrypt ntdll)
endif()
target_compile_definitions(cxxLibrary PUBLIC
BOOST_ALL_NO_LIB
BOOST_ALL_NO_LIBRARY
BOOST_FILESYSTEM_NO_CXX20_ATOMIC_REF
)
set_target_properties(cxxLibrary PROPERTIES DEBUG_POSTFIX "d"
# 可选:设置 Release 版本后缀
set_target_properties(cxxLibrary PROPERTIES
DEBUG_POSTFIX "d"
# 可选:设置其他配置的后缀
# RELEASE_POSTFIX ""
# RELWITHDEBINFO_POSTFIX "rd"
# MINSIZEREL_POSTFIX "s"
)
# 修正2:添加头文件安装
# 如果include目录中有头文件,需要单独安装它们
install(DIRECTORY include/
DESTINATION include
FILES_MATCHING PATTERN "*.h"
PATTERN "*.hpp"
)
# 安装目标
install(TARGETS cxxLibrary
EXPORT cxxLibraryTargets # 用于生成导出文件
ARCHIVE DESTINATION lib # 静态库安装到lib目录
LIBRARY DESTINATION lib # 动态库安装到lib目录
RUNTIME DESTINATION bin # Windows的DLL
INCLUDES DESTINATION include # 头文件安装位置
)
# 安装导出文件
install(EXPORT cxxLibraryTargets
FILE cxxLibraryTargets.cmake
NAMESPACE cxxLibrary:: # 添加命名空间
DESTINATION lib/cmake/cxxLibrary
)
include(CMakePackageConfigHelpers)
# 生成配置文件
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cxxLibrary.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfig.cmake"
INSTALL_DESTINATION lib/cmake/cxxLibrary
)
# 生成版本文件
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
# 安装生成的文件
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfigVersion.cmake"
DESTINATION lib/cmake/cxxLibrary
)

16
cmake/cxxLibrary.cmake.in Normal file
View File

@@ -0,0 +1,16 @@
@PACKAGE_INIT@
# 包含导出的目标文件
include("${CMAKE_CURRENT_LIST_DIR}/cxxLibraryTargets.cmake")
# 手动设置包含路径
get_target_property(cxxLibrary_INCLUDE_DIRS cxxLibrary::cxxLibrary INTERFACE_INCLUDE_DIRECTORIES)
if(NOT cxxLibrary_INCLUDE_DIRS)
set(cxxLibrary_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include")
endif()
# 设置找到的变量
set(cxxLibrary_VERSION @PROJECT_VERSION@)
set(cxxLibrary_VERSION_STRING "@PROJECT_VERSION@")
set(cxxLibrary_FOUND TRUE)
message(STATUS "Found cxxLibrary version ${cxxLibrary_VERSION}")