From 4419d606a805b7523a0e89be17b47ef3c4db908b Mon Sep 17 00:00:00 2001 From: taynpg Date: Fri, 27 Mar 2026 22:39:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0msvc=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BuildVs.bat | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 10 +++++-- 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 BuildVs.bat diff --git a/BuildVs.bat b/BuildVs.bat new file mode 100644 index 0000000..64229ba --- /dev/null +++ b/BuildVs.bat @@ -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="Ninja" + +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 \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f250d3..c03b05c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,10 @@ cmake_minimum_required(VERSION 3.16) project(cxxLibrary VERSION 1.0.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) +if (MSVC) + add_compile_definitions(-D_WIN32_WINNT=0x0601) +endif() + set(SOURCES src/tinyxml2.cpp ) @@ -16,7 +20,7 @@ file(GLOB_RECURSE BOOST_SOURCES add_library(cxxLibrary STATIC ${SOURCES} ${BOOST_SOURCES}) # 修正1:使用相对路径,避免绝对路径警告 -target_include_directories(cxxLibrary PUBLIC +target_include_directories(cxxLibrary PUBLIC $ $ ) @@ -31,7 +35,7 @@ target_compile_definitions(cxxLibrary PUBLIC BOOST_FILESYSTEM_NO_CXX20_ATOMIC_REF ) -set_target_properties(cxxLibrary PROPERTIES +set_target_properties(cxxLibrary PROPERTIES DEBUG_POSTFIX "d" # 可选:设置其他配置的后缀 # RELEASE_POSTFIX "" @@ -84,4 +88,4 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/cxxLibraryConfigVersion.cmake" DESTINATION lib/cmake/cxxLibrary -) \ No newline at end of file +)