5

I'm trying to use Visual Studio as an editor for browsing a large open source project on GitHub. The directory structure looks like

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

and build is maintained by CMakeLists.txt.

If I insist to use Visual Studio as an editor for some reason (for example, good IntelliSense support), what would be the best practice?

  1. I tried "New - Project from Existing Code". It produces ugly project structure where all *.cpp files are under Source Files filter while I still need to manually specify a bunch of /*/include/ directories.

  2. I tried to actually use CMake to produce a visual studio solution. It immediately failed with many critical errors because of a lot of Linux dependencies.

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

D. Fisher
  • 223
  • 2
  • 12
  • Can you please add some details on the "open source project" you are referring to? And what error messages do you get with CMake? Generally speaking and without knowing what exactly your targeted environment is: if you want to use Visual Studio as the IDE for developing `GNU` toolchain based projects there are commercial tools available like [VisualGDB](http://visualgdb.com/) or [WinGDB](http://wingdb.com/). – Florian Sep 18 '15 at 08:16
  • The project contains a bunch of pre- and post-build bash scripts running Linux executables, so I don't believe I can use VS to compile the project. I'm looking for a way to easily create a VS solution that IntelliSense is enabled. The project is huge: a hundred subdirectories containing their own include/ and src/. – D. Fisher Sep 18 '15 at 08:38
  • It is more realistic to use CLion at this stage before Microsoft adds specific support for CMake. There is an add on for VS called CMake Tools for Visual Studio but its quality is up to your own testing. – Lex Li Sep 18 '15 at 09:28

1 Answers1

5

I see four possible approaches:

  1. Using a commercial product like Visual GDB or WinGDB you could e.g. import remote Linux projects to Visual Studio.

  2. 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 seems to require a newer version of Visual Studio > 2012).

    With "Show All Files" and "Include in Project" I was able to get all sources/headers with their directory structure (tested with Visual Studio 2013/2015).

  3. You could mock all functions beside the most basic ones (like add_library() or message()) and try to get the original CMake project to generate a Visual Studio solution. E.g. you make 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 this approach has it's limits.

  4. You're writing a special main CMakeLists.txt to collect all sources/headers into a new solution like:

    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 root of the sources; but you could 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 nested sub directory using cmake).

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • As of CMake 3.8, the [`source_group`](https://cmake.org/cmake/help/v3.8/command/source_group.html) command has a `TREE` argument to recursively map source files to source groups. The `foreach()` can be simplified to a one-liner: `source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${_source_list})` – Kevin Jun 07 '19 at 14:40