• CMake for Visual Studio


    Property Sheet

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

    
    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
      
      <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>    
    
    • 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

    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(
        $<$:/MD>
        $<$:/MDd>
        $<$:/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}/.
    )
    
    • 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

    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
    
    • 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

    Build

    with cmake

    from cmd with cmake --build

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

    with Visual Studio

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

    Ref

  • 相关阅读:
    商家团购app微信小程序模板
    轻量应用服务器和云服务器有哪些区别?该如何选择?
    SpringBoot+Vue的社区疫情防控管理系统|基于Python+Django的社区物资采购系统
    JAVA之springMVC
    vue.js毕业设计,基于vue.js前后端分离外卖点餐系统(H5移动项目) 开题报告
    随笔感悟:Mysql悲观锁和乐观锁
    2019阿里java面试题
    震惊!CSS 也能实现碰撞检测?
    Metabase学习教程:系统管理-3
    监控电脑的软件叫什么丨科普小知识
  • 原文地址:https://blog.csdn.net/u011178262/article/details/127855933