Visual Studio作为CMake友好项目的编辑器 [英] Visual Studio as an editor for CMake friendly project
问题描述
我试图使用Visual Studio作为编辑器来浏览GitHub上的一个大型开源项目。目录结构看起来像
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/
...
由CMakeLists.txt维护。
and build is maintained by CMakeLists.txt.
如果由于某种原因(例如,良好的IntelliSense支持),我坚持使用Visual Studio作为编辑器,最好的做法是什么?
If I insist to use Visual Studio as an editor for some reason (for example, good IntelliSense support), what would be the best practice?
-
我尝试了New - Project from Existing Code。它产生丑陋的项目结构,其中所有的* .cpp文件在源文件过滤器下,而我仍然需要手动指定一堆/ * / include /目录。
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.
我试图实际使用CMake来产生一个视觉工作室的解决方案。它立即失败了许多关键错误,因为很多Linux依赖。
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?
推荐答案
我看到四种可能的方法:
I see four possible approaches:
-
使用 Visual GDB 或< a href =http://wingdb.com/ =nofollow> WinGDB 你可以eg 将远程Linux项目导入Visual Studio 。
您在源的根目录创建一个新的空C ++项目,并使用在Visual Studio 2012中导入现有的源文件(但这似乎需要更高版本的Visual Studio> 2012)。
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).
使用显示所有文件和包括在项目我能够获得所有的源/标题与其目录结构(用Visual Studio 2013/2015测试)。
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).
您可以模拟最基本的功能旁边的所有功能(例如 add_library()
$ c> message()),并尝试获取原始的 CMake
项目来生成Visual Studio解决方案。例如。您可以拥有 VSToolchain.cmake
或 PreLoad.cmake
:
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()
...
限制。
您正在撰写一个特殊主要的 CMakeLists.txt
一个新的解决方案如下:
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()
只是把它放在某处(在我的例子中是源的根;但是你可以改变 _src_root_path
)并生成一个Visual Studio项目,包含你的源/目录结构的头文件(见如何使用cmake 为嵌套子目录设置Visual Studio过滤器。)
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).
这篇关于Visual Studio作为CMake友好项目的编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!