From 8de1dfb3aa3ded62f8c568810f91f6c8719dd0be Mon Sep 17 00:00:00 2001 From: wdp <544209413@qq.com> Date: Sat, 18 Jan 2025 19:46:56 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=B7=BB=E5=8A=A0=E3=80=91Windows?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E9=85=8D=E7=BD=AE=E5=8F=8ACMake=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 19 +++++++++++++++++++ .vscode/tasks.json | 10 +++++++--- CMakeLists.txt | 21 +++++++++++++-------- build-win-app.ps1 | 20 ++++++++++++++------ 4 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..8ae0264 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/3rd/**", + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "compilerPath": "C:\\MinGW\\bin\\gcc.exe", + "intelliSenseMode": "windows-gcc-x86" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a6f33ea..3030132 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -11,7 +11,9 @@ "-build_type", "Release", "-build_and_run", - "false" + "false", + "-platform", + "win" ], "group": { "kind": "build", @@ -28,11 +30,13 @@ "command": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "args": [ "-File", - "${workspaceFolder}\\build.ps1", + "${workspaceFolder}\\build-win-app.ps1", "-build_type", "Debug", "-build_and_run", - "true" + "true", + "-platform", + "win" ], "group": { "kind": "build", diff --git a/CMakeLists.txt b/CMakeLists.txt index af7956d..6ae4476 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,14 +3,19 @@ project(Demo) set(CMAKE_CXX_STANDARD 11) if(WIN32) set(CMAKE_CXX_COMPILER "MSVC") + elseif(UNIX) set(CMAKE_CXX_COMPILER "g++") endif() -# 指定库链接目录 -link_directories(3rd/lib) +# 根据 platform 值设置 CMake 变量 +if(${CMAKE_BUILD_PLATFORM} STREQUAL "win") + link_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rd/win/opencv/x64/vc15/lib) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rd/win/opencv/include) +else() + message(FATAL_ERROR "不支持的平台: ${CMAKE_BUILD_PLATFORM}") +endif() -include_directories(3rd/include) include_directories(src) aux_source_directory(./src DIR_SRCS) @@ -19,8 +24,8 @@ aux_source_directory(./app DIR_SRCS) add_executable(Demo ${DIR_SRCS}) -# if(CMAKE_BUILD_TYPE STREQUAL "Debug") -# target_link_libraries(${PROJECT_NAME} spdlogd-mt-x64.lib) -# else() -# target_link_libraries(${PROJECT_NAME} spdlog-mt-x64) -# endif() +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_link_libraries(${PROJECT_NAME} opencv_world430d) +else() + target_link_libraries(${PROJECT_NAME} opencv_world430) +endif() diff --git a/build-win-app.ps1 b/build-win-app.ps1 index 8cc2683..0ad12e6 100644 --- a/build-win-app.ps1 +++ b/build-win-app.ps1 @@ -3,7 +3,10 @@ param( [string]$build_type, [Parameter(Mandatory=$true)] - [string]$build_and_run + [string]$build_and_run, + + [Parameter(Mandatory=$true)] + [string]$platform ) # 定义项目和构建目录的变量 @@ -20,11 +23,17 @@ if (-Not (Test-Path -Path $buildDir)) { Set-Location -Path $buildDir # 运行CMake生成构建系统 -# 这里假设CMakeLists.txt在项目根目录下 -cmake -G "Visual Studio 17 2022" -T host=x64 -DCMAKE_BUILD_TYPE=$build_type .. +cmake -G "Visual Studio 17 2022" -T host=x64 -DCMAKE_BUILD_TYPE="$build_type" -DCMAKE_BUILD_PLATFORM="$platform" .. + # 编译项目 cmake --build . --config $build_type +# 检查编译命令的退出代码 +if ($LASTEXITCODE -ne 0) { + Write-Host "CMake build failed with exit code $LASTEXITCODE. Exiting script." -ForegroundColor Red + exit $LASTEXITCODE +} + # 返回项目目录 Set-Location -Path $projectDir @@ -33,7 +42,6 @@ Write-Host "build done." -ForegroundColor Darkgreen if($build_and_run -eq "true"){ Write-Host "################## APP Run ##################" -ForegroundColor Blue - Start-Process -FilePath "build\\${build_type}\\TestCode.exe" - # & "build\\$build_type\\TestCode.exe" - + $env:PATH += ";3rd\win\opencv\x64\vc15\bin" + Start-Process -FilePath "build\\${build_type}\\Demo.exe" }