Visual Studio作为一个CMake友好项目的编辑器

5

我正在尝试使用Visual Studio作为GitHub上一个大型开源项目的编辑器。目录结构如下:

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

而且构建由CMakeLists.txt维护。

如果出于某些原因(例如良好的智能感知支持),我坚持使用Visual Studio作为编辑器,那么最佳实践是什么?

  1. 我尝试了“新建 - 从现有代码创建项目”。它生成了丑陋的项目结构,其中所有*.cpp文件都在源文件过滤器下,而我仍然需要手动指定一堆/* /include/目录。

  2. 我尝试使用CMake来生成Visual Studio解决方案。立即由于许多Linux依赖项而失败。

是否有办法创建具有正确目录结构和包含路径的Visual Studio解决方案?


请问您能否添加一些关于您所提到的“开源项目”的细节呢?以及您在使用 CMake 时遇到了哪些错误信息?一般而言,如果您想要将 Visual Studio 作为 IDE 来开发基于 GNU 工具链的项目,商业工具是可用的,例如 VisualGDB 或者 WinGDB - Florian
该项目包含一堆预构建和后构建的Bash脚本,运行Linux可执行文件,因此我不认为我可以使用VS编译该项目。我正在寻找一种简单的方法来创建启用IntelliSense的VS解决方案。该项目非常庞大:有数百个子目录,包含自己的include/和src/。 - D. Fisher
在微软添加对CMake的特定支持之前,在这个阶段使用CLion更为现实。Visual Studio有一个名为CMake Tools的插件,但其质量取决于您自己的测试。 - Lex Li
1个回答

5
我看到有四种可能的方法:

第一种方法是……

  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).


从CMake 3.8开始,source_group命令具有TREE参数,可以递归地将源文件映射到源组。 foreach()可以简化为一行代码:source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${_source_list}) - Kevin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接