cmake:如何创建视觉工作室过滤器 [英] cmake: how to create visual studio filters

查看:209
本文介绍了cmake:如何创建视觉工作室过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过了(StackOverflow和更多),我尝试使用cmake生成Visual Studio过滤器。 ie ...我有以下文件夹:



src / math
src / import
src / ui



,我想生成像



math这样的过滤器:包含所有的cpp& h文件在src / math中



import:包含所有cpp& h文件在src / import



ui:包含所有cpp& h文件在src / ui



我尝试了severak解决方案,但没有一个似乎工作!!!



这是CMakeList.txt中的最后一个版本的代码:

  set(VD_SRC$ {VisualDesigner_SOURCE_DIR} / src / visualdesigner )

文件(GLOB_RECURSE SRC_UI
$ {VD_SRC} / ui / *。cpp,$ {VD_SRC} / ui / *。h)
文件(GLOB_RECURSE SRC_IMPORT
$ {VD_SRC} / import / *。cpp,
$ {VD_SRC} / import / *。h)

source_group(uiFILES $ {SRC_UI})
source_group(importFILES $ {SRC_IMPORT})

欢迎!

解决方案

请参阅



这是一个更通用的版本,取自 Visual Studio作为编辑器CMake友好项目

 设置(_src_root_path$ {VisualDesigner_SOURCE_DIR} / src / visualdesigner)
文件(
GLOB_RECURSE _source_list
LIST_DIRECTORIES false
$ {_ src_root_path} / *。c *
$ {_ src_root_path} / *。h *


add_library(VisualDesigner $ {_ source_list})

foreach(_source IN ITEMS $ {_ source_list})
get_filename_component(_source_path$ {_ source}PATH)
文件(RELATIVE_PATH _source_path_rel$ {_ src_root_path}$ {_ source_path})
string(REPLACE/\\_group_path$ {_ source_path_rel})
source_group $ {_ group_path}FILES$ {_ source})
endforeach()


I have already look around (StackOverflow and more) and I try to use cmake to generate Visual Studio filters. ie ... I have the following folders :

src/math src/import src/ui

and I would like to generate the filters like thos

"math" : contains all the cpp & h files in src/math

"import" : contains all the cpp & h files in src/import

"ui" : contains all the cpp & h files in src/ui

I have try severak solution, but none seems to work !!!

Here is the last version of the code in CMakeList.txt :

set(VD_SRC "${VisualDesigner_SOURCE_DIR}/src/visualdesigner")

file(GLOB_RECURSE SRC_UI
    "${VD_SRC}/ui/*.cpp", "${VD_SRC}/ui/*.h")
file(GLOB_RECURSE SRC_IMPORT
    "${VD_SRC}/import/*.cpp",
    "${VD_SRC}/import/*.h")

source_group("ui"            FILES ${SRC_UI})
source_group("import"        FILES ${SRC_IMPORT})

Any help is welcomed !

解决方案

See How to set Visual Studio Filters for nested sub directory using cmake

Just be aware that

  • the source_group() command only works in combination with add_library() or add_executable() commands listing the same sources (the paths must match)
  • the source_group() command does not check if the file actually exists (so it takes anything you give it and during project file generation it tries to match the given source group file names against files used in the project)

I have given your code a try by adding a corresponding add_library() target and it works as expected (CMake 3.3.2 and VS2015):

set(VD_SRC "${VisualDesigner_SOURCE_DIR}/src/visualdesigner")

file(GLOB_RECURSE SRC_UI
    "${VD_SRC}/ui/*.cpp"
    "${VD_SRC}/ui/*.h"
)
file(GLOB_RECURSE SRC_IMPORT
    "${VD_SRC}/import/*.cpp"
    "${VD_SRC}/import/*.h"
)

add_library(VisalDesigner ${SRC_UI} ${SRC_IMPORT})

source_group("ui"            FILES ${SRC_UI})
source_group("import"        FILES ${SRC_IMPORT})

Results in

Here is a more generalized version taken from Visual Studio as an editor for CMake friendly project:

set(_src_root_path "${VisualDesigner_SOURCE_DIR}/src/visualdesigner")
file(
    GLOB_RECURSE _source_list 
    LIST_DIRECTORIES false
    "${_src_root_path}/*.c*"
    "${_src_root_path}/*.h*"
)

add_library(VisualDesigner ${_source_list})

foreach(_source IN ITEMS ${_source_list})
    get_filename_component(_source_path "${_source}" PATH)
    file(RELATIVE_PATH _source_path_rel "${_src_root_path}" "${_source_path}")
    string(REPLACE "/" "\\" _group_path "${_source_path_rel}")
    source_group("${_group_path}" FILES "${_source}")
endforeach()

这篇关于cmake:如何创建视觉工作室过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆