[CMake] Keep folder structure in Visual Studio Project generated with CMake

Magnus Therning magnus at therning.org
Tue Nov 12 08:03:51 EST 2013


When I am forced to use VS for development work I tend to only use it
as an editor and bild tool, i.e. I don't create files in it or add
things to the project.  Instead the sequence I use to add a file is:

1. touch the file I want created
2. modify the relevant CMakeLists.txt to include the generated file
3. re-generate the VS solution and projects files (either by running
CMake directly or by building inside VS)
4. answer 'yes' to all questions about reloading solution and projects in VS
5. open the file, which now will have appeared in the solution explorer in VS
6. hack away

It's maybe a few too many steps for someone used to working with only
VS, but the power, happiness, and bliss of using CMake doesn't come
completely free ;)

/M


On Tue, Nov 12, 2013 at 11:44 AM, Luca Gherardi <luca.gherardi at unibg.it> wrote:
> I have a projects in which the sources file are organizes in a hierarchy of
> directories.
>
> For example:
>
> - Src
>
> | - Folder A
>
>    | - 1.cpp
>
>    | - 1.hpp
>
>    | - FolderA1
>
>       | - 2.cpp
>
>       | - 2.hpp
>
> | - Folder B
>
>    | - 3.cpp
>
>    | - 3.hpp
>
>
>
>
>
> I wrote a CMakeLists.txt for generating a Visual Studio Projects (VS 2012).
>
>
>
> My goals are the following:
>
> -        Preserve the directory structure
>
> -        Allows other developer to add source files in the directory, by
> means of visual studio, without having to write CMakeLists.txt
>
>
>
> For doing that I wrote the following CMakeLists.txt
>
>
>
> cmake_minimum_required(VERSION 2.8.11)
>
> project(Common-library CXX)
>
> #set the default path for built executables to the "bin" directory
>
> set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
>
> #set the default path for built libraries to the "lib" directory
>
> set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
>
>
>
> #Specify my Include Directory
>
> set(PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src)
>
>
>
> # Boost configuration
>
> SET(Boost_USE_STATIC_LIBS ON)
>
> find_package(Boost REQUIRED COMPONENTS filesystem system thread REQUIRED)
>
>
>
> include_directories( ${Boost_INCLUDE_DIRS} ${ PROJECT_INCLUDE_DIR} )
>
>
>
> # I used this for allowing CMake to include all the files,
>
> # also new ones, without having to touch the CMakeLists.txt
>
> file(GLOB_RECURSE PROJECT_SOURCE_FILES "*.h" "*.hpp" "*.cpp")
>
>
>
> # I then wrote this Macro, which organize the files in visual studio
> folders,
>
> # according to the organization in the file systems
>
>
>
> macro(GroupSources curdir)
>
>    file(GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir}
> ${PROJECT_SOURCE_DIR}/${curdir}/*)
>
>    foreach(child ${children})
>
>           if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child})
>
>                   GroupSources(${curdir}/${child})
>
>           else()
>
>                   string(REPLACE "/" "\\" groupname ${curdir})
>
>           # I would like to call the src root folder in a different name,
> only in visual studio (not mandatory requirement)
>
>                    string(REPLACE "src" "Common" groupname ${groupname})
>
>             source_group(${groupname} FILES
> ${PROJECT_SOURCE_DIR}/${curdir}/${child})
>
>           endif()
>
>    endforeach()
>
> endmacro()
>
>
>
> # Execute the macro
>
> GroupSources(src)
>
>
>
> add_library(${PROJECT_NAME} ${ PROJECT_SOURCE_FILES})
>
>
>
>
>
> The result is a Visual studio project which organizes the files as I
> desired.
>
> However when I try to create new files with visual studio they are not
> stored in the folder I select, but in the out-of-source build folder.
>
> I guess this is due to the fact that visual studio doesn’t now the mapping
> between src folder and source group.
>
>
>
> How can I achieve both my goals?
>
>
>
> Thanks in advace!
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4
email: magnus at therning.org   jabber: magnus at therning.org
twitter: magthe               http://therning.org/magnus


More information about the CMake mailing list