[CMake] Keep folder structure in Visual Studio Project generated with CMake
Luca Gherardi
luca.gherardi at unibg.it
Tue Nov 12 05:44:39 EST 2013
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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20131112/5ae87e43/attachment-0001.htm>
More information about the CMake
mailing list