zoukankan      html  css  js  c++  java
  • CMake在Visual Studio下保持目录结构

    CMake在Visual Studio下保持目录结构

    原理

    主要通过CMAKE自带函数source_group来设定。
    需要把add_executable()函数进行封装,包裹一层source_group()的处理

    例子

    现有目录结构

    hello/include/hello.hpp
    hello/src/hello.cpp
    hello/CMakeLists.txt
    

    编写CMakeLists.txt

    cmake_minimum_required(VERSION 3.1)
    
    project(hello)
    
    function(assign_source_group)
        foreach(_source IN ITEMS ${ARGN})
            if (IS_ABSOLUTE "${_source}")
                file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_source}")
            else()
                set(_source_rel "${_source}")
            endif()
            get_filename_component(_source_path "${_source_rel}" PATH)
            string(REPLACE "/" "\" _source_path_msvc "${_source_path}")
            source_group("${_source_path_msvc}" FILES "${_source}")
        endforeach()
    endfunction(assign_source_group)
    
    function(my_add_executable)
    	foreach(_source IN ITEMS ${ARGN})
    		assign_source_group(${_source})
    	endforeach()
    	add_executable(${ARGV})
    endfunction(my_add_executable)
    
    my_add_executable(hello include/hello.hpp src/hello.cpp)
    

    执行编译

    cd hello
    mkdir build
    cd build
    cmake .. -G "Visual Studio 14 Win64"
    cmake --build .
    

    打开hello/build/hello.sln看看,是不是好了!

    Reference

    https://stackoverflow.com/questions/31422680/how-to-set-visual-studio-filters-for-nested-sub-directory-using-cmake

  • 相关阅读:
    绘制程序流程图笔记
    强软弱虚引用
    安全点和安全区域
    垃圾回收算法
    垃圾回收相关算法
    内存访问全过程
    多级页表与快表
    分页
    虚拟内存
    内存分段机制
  • 原文地址:https://www.cnblogs.com/zjutzz/p/7284114.html
Copyright © 2011-2022 走看看