文章目录
Property Sheet
example main.props, 在此文件 设置工程,方便分享
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- 添加其他props文件 -->
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>
<!-- 头文件路径 -->
</IncludePath>
<LibraryPath>
<!-- 库文件路径 -->
$(LibraryPath)
</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>
<!-- 库文件 -->
%(AdditionalDependencies)
</AdditionalDependencies>
<AdditionalLibraryDirectories>
<!-- 库文件路径 -->
</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
CMakeLists.txt
example file
cmake_minimum_required(VERSION 3.0.0)
project(ProjName C CXX)
set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "Possible configurations" FORCE)
if (DEFINED CMAKE_BUILD_TYPE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES})
endif()
if(MSVC)
# Dynamically link the runtime libraries
add_compile_options(
$<$<CONFIG:>:/MD>
$<$<CONFIG:Debug>:/MDd>
$<$<CONFIG:Release>:/MD>
)
# 设置 VisualStudio 链接器->输入 链接库 “从父继承”
set(CMAKE_CXX_STANDARD_LIBRARIES "$(CMAKE_CXX_STANDARD_LIBRARIES) %(AdditionalDependencies)")
endif()
set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/install)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/lib/)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}/bin/)
add_definitions(-w)
if(MSVC)
#remove_definitions(/W3)
#add_definitions(/W4)
add_definitions(/wd4819)
add_definitions(/wd4244)
add_definitions(/wd4267)
add_definitions(-DNOMINMAX)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()
include_directories()
# Visual Studio 分组
SOURCE_GROUP("" FILES ${Root_FILES})
SOURCE_GROUP(Dir\\SubDir FILES ${SubDir_FILES})
add_executable(foo)
target_link_libraries(foo ${LIBS})
# set_target_properties(foo PROPERTIES COMPILE_FLAGS "/Od")
# 添加 自定义PropertySheet文件 到 Visual Studio
set_target_properties(foo PROPERTIES VS_USER_PROPS "${PROJECT_SOURCE_DIR}/main.props")
# set_target_properties(foo PROPERTIES LINK_LIBRARIES "%(AdditionalDependencies)")
# Visual Studio 后期生成事件
ADD_CUSTOM_COMMAND(
TARGET foo
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy ${EXAMPLE_BIN_NAME} ${PROJECT_BINARY_DIR}/.
)
Building System for Visual Studio
example Windows Batch file config.bat
:: Create configuration for Visual Studio for Release and Debug modes.
@echo off
set ARCH=x64
set SRC_DIR=.\
set BUILD_DIR=%SRC_DIR%/build%ARCH%
echo.
echo PROCESSOR_ARCHITECTURE: %PROCESSOR_ARCHITECTURE%
echo.
echo ============= Checking for CMake ============
echo.
cmake --version
if not %errorlevel% == 0 (
echo Error: CMake not found, please install it - see http://www.cmake.org/
exit /B 1
)
echo.
echo ============= Creating build system for Visual Studio ============
echo.
if exist %BUILD_DIR% (
echo delete
rmdir /s %BUILD_DIR%
)
set MSVCDIR=%VS140COMNTOOLS%..\..\VC
:: Configure environment for Visual Studio
call "%MSVCDIR%\VCVARSALL.BAT" %ARCH%
set CMAKE_VS_GENERATOR=Visual Studio 14 2015 Win64
echo Using cmake generator %CMAKE_VS_GENERATOR%
set cmake_generator_options=-G "%CMAKE_VS_GENERATOR%"
if "%CMAKE_VS_GENERATOR_TOOLSET%" neq "" (
echo Using cmake generator toolset %CMAKE_VS_GENERATOR_TOOLSET%
set cmake_generator_options=%cmake_generator_options% -T "%CMAKE_VS_GENERATOR_TOOLSET%"
)
:: copy CMakeLists.txt ..
::set cmake_debug_options=--trace --debug-output
cmake -S %SRC_DIR% -B %BUILD_DIR% -Wno-dev %cmake_debug_options% %cmake_generator_options% || exit /B 1
echo.
echo ============= Building ============
echo.
echo To build:
echo - go to %BUILD_DIR%
echo - run 'cmake --build %BUILD_DIR% --parallel 3 --config Release(or Debug) [--target target_to_build]'
echo.
exit /B 0
Build
with cmake
from cmd with cmake --build
cmake --build %build-dir% --parallel 3 --config Release
with Visual Studio
打开 Visual Studio 工程 xxx.sln,生成解决方案

本文详细介绍了如何使用CMake构建VisualStudio项目,包括PropertySheet的配置、CMakeLists.txt的编写以及VisualStudio的构建设置。通过示例文件展示了如何设置头文件路径、库文件路径、额外依赖项,并提供了用于VisualStudio的自定义PropertySheet。同时,还给出了用于创建和配置VisualStudio构建系统的批处理脚本config.bat。
332

被折叠的 条评论
为什么被折叠?



