Visual Studio as an editor for a project with CMake support

I am trying to use Visual Studio as an editor to view a large open source project on GitHub. The directory structure looks like

/module1/include/ /module1/src/ /module2/include/ /module2/src/ ... 

and the assembly is supported by CMakeLists.txt.

If I highly recommend using Visual Studio as an editor for any reason (e.g. good IntelliSense support), what would be the best practice?

  • I tried "Create - project from existing code." It creates an ugly project structure in which all * .cpp files are in the Source File Filter section, while I still need to manually specify the / * / include / directories group.

  • I tried using CMake to create a visual studio solution. It immediately failed with many critical errors due to the large number of Linux dependencies.

Is there a way to create a visual studio solution with an appropriate directory structure and include paths?

+3
editor visual-studio cmake
source share
1 answer

I see four possible approaches:

  • Using a commercial product such as Visual GDB or WinGDB, you could, for example, import remote Linux projects into Visual Studio .

  • You create a new empty C ++ project at the root of your sources and use the trick described in Importing an Existing Source File in Visual Studio 2012 (but this, apparently, requires a newer version of Visual Studio> 2012).

    Using "Show all files" and "Include in project" I was able to get all the sources / headers with their directory structure (tested using Visual Studio 2013/2015).

  • You can make fun of all the functions next to the most basic ones (for example, add_library() or message() ) and try to get the original CMake project to create a Visual Studio solution. For example. you create your own VSToolchain.cmake or PreLoad.cmake with empty implementations:

     macro(add_custom_command) endmacro() macro(add_custom_target) endmacro() macro(set_property) endmacro() ... 

    I admit that this approach limits it.

  • You write a special core CMakeLists.txt to collect all sources / headers into a new solution, for example:

     cmake_minimum_required(VERSION 2.8) project(MyProject C CXX) set(_src_root_path "${CMAKE_CURRENT_SOURCE_DIR}") file( GLOB_RECURSE _source_list LIST_DIRECTORIES false RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${_src_root_path}/*.c*" "${_src_root_path}/*.h*" ) add_library(MySources ${_source_list}) foreach(_source IN ITEMS ${_source_list}) get_filename_component(_source_path "${_source}" PATH) string(REPLACE "/" "\\" _source_path_msvc "${_source_path}") source_group("${_source_path_msvc}" FILES "${_source}") endforeach() 

    Just put it somewhere (in my example, the source root, but you can just change _src_root_path ) and generate a Visual Studio project containing your sources / headers structured by directories (see How to set Visual Studio filters for a nested subdirectory using cmake )

+4
source share

All Articles