`
- 浏览:
1159354 次
-
CMake Tips - 在Visual Studio中按目录结构自动创建Filters(Source Groups)
CMake作为构建系统可以轻松生成Visual Studio工程,这让我们的新项目可以不必维护一组专门的Visual Studio工程文件和sln文件了。但是CMake默认生成的工程文件中,只是笼统区分了头文件和其他源文件。对于大型项目,要在众多的源文件中找到想要的那个是很恼火的事情。对此,Visual Studio提供了filter机制来分类显示源文件。对于手工维护工程文件的项目,我们可以自己添加filter,然后提交到代码库,并与其他成员共享。
但是,在使用CMake后,手工维护filter是不现实的。CMake也提供了可以生成filter的机制,就是source_group()命令。结合source_group(), file(), string()等命令,我们可以让实现CMake自动按目录结构生成filter。
方法很简单,代码如下:
macro(source_group_by_dir source_files)
if(MSVC)
set(sgbd_cur_dir ${CMAKE_CURRENT_SOURCE_DIR})
foreach(sgbd_file ${${source_files}})
string(REGEX REPLACE ${sgbd_cur_dir}/\(.*\) \\1 sgbd_fpath ${sgbd_file})
string(REGEX REPLACE "\(.*\)/.*" \\1 sgbd_group_name ${sgbd_fpath})
string(COMPARE EQUAL ${sgbd_fpath} ${sgbd_group_name} sgbd_nogroup)
string(REPLACE "/" "\\" sgbd_group_name ${sgbd_group_name})
if(sgbd_nogroup)
set(sgbd_group_name "\\")
endif(sgbd_nogroup)
source_group(${sgbd_group_name} FILES ${sgbd_file})
endforeach(sgbd_file)
endif(MSVC)
endmacro(source_group_by_dir)
使用的时候,先在比较顶层的CMakeLists.txt中定义该宏。然后在添加工程(add_library或者add_executable)的CMakeLists.txt文件中调用该宏:
source_group_by_dir(all_files)
其中all_files是保存了所有文件名的变量。注意,这里用的是变量名,而没有引用其值。
一般这个文件列表可以用file()或者aux_source_directory()来得到。例如对于C++工程,通常是这样的:
file(GLOB_RECURSE project_headers *.h)
file(GLOB_RECURSE project_cpps *.cpp)
set(all_files ${project_headers} ${project_cpps})
source_group_by_dir(all_files)
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
coin-or/CoinUtils-cmake-2.11.6 Osi-cmake-0.108.7 Clp-cmake-1.17.7 Cgl-cmake-0.60.5 Cbc-cmake-2.10.7源码+CMakeLists.txt,解决了源码没有CMake的问题。 Cbc(Coin或branch and cut)是一个开源的混合整数线性...
cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64
cmake-3.18.0-rc2-win64-x64.msi 安装版
devcpp-cmake-template-devcpp-sdl-templatedevcpp-cmake-template-devcpp-sdl-templatedevcpp-cmake-template-devcpp-sdl-templatedevcpp-cmake-template-devcpp-sdl-templatedevcpp-cmake-template-devcpp-sdl-...
cmake-3.16.0-Linux-x86_64.tar.gz; cmake-3.16.0-Linux-x86_64.tar.gz;
Cmake-2.8.8-win32-x86 CmakeCmake-2.8.8-win32-x86 CmakeCmake-2.8.8-win32-x86 CmakeCmake-2.8.8-win32-x86 CmakeCmake-2.8.8-win32-x86 CmakeCmake-2.8.8-win32-x86 Cmake
cmake-3.11.0-rc2-win64-x64.zip,直接解压安装就可以用
cmake-3.15.0-win64-x64.msi 官网下载地址:https://cmake.org/files/
linux下安装cmake安装脚本: 方法如下: 1:sudo chmod +x cmake-3.27.6-linux-x86_64.sh 2:sudo sh cmake-3.27.6-linux-x86_64.sh --skip-license --prefix=/usr/local/
CMake 支援 in-place 建构(二进档和源代码在同一个目录树中)和 out-of-place 建构(二进档在别的目录里),因此可以很容易从同一个源代码目录树中建构出多个二进档。CMake 也支援静态与动态程式库的建构。 “CMake...
CMake3.18.0-win7-64安装版和免安装包.zip
cmake-3.10.0-rc4-win64-x64.msi cmake-3.10.0-rc4-win64-x64.msi cmake-3.10.0-rc4-win64-x64.msi
离线安装包,亲测可用
cmake-3.16.3-win64-x64。CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake...
cmake-3.10.2-Linux-x86_64.tar.gz
名称:cmake-3.23.2-windows-x86_64.msi 版本:3.23.2 文件类型:Windows安装程序(.msi) 描述: cmake-3.23.2-windows-x86_64.msi是用于Windows操作系统的CMake构建工具的安装程序文件。CMake是一个开源跨平台的...
cmake-3.13.0-rc2,打开直接就能用,自己已经亲自测试用过了。
cmake-3.18.0-Linux-x86_64.tar.gz 官网目前最新版 tar.gz格式的文件
CMake is a cross-platform, open-source build system generator. For full documentation visit the CMake Home Page and the CMake Documentation Page. The CMake Community Wiki also references useful guides...
cmake-3.15.0-win32-x86.msi 官网下载地址:https://cmake.org/files/