CMake for Visual Studio

Last updated on May 7, 2023 pm

[TOC]

Property Sheet

example main.props, 在此文件 设置工程,方便分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
:: 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

1
cmake --build %build-dir% --parallel 3 --config Release

with Visual Studio

打开 Visual Studio 工程 xxx.sln,生成解决方案

Ref


CMake for Visual Studio
https://cgabc.xyz/posts/449abec3/
Author
Gavin Gao
Posted on
June 1, 2022
Licensed under