简体   繁体   English

Visual Studio构建托管在GitHub中的C ++ / CMake项目

[英]Visual Studio to build C++/CMake project hosted in GitHub

I am an "emacs/[c]make/ninja/clang/bash/linux/macOs" kind of C++ developer who is not used to IDEs like Visual Studio. 我是“ emacs / [c] make / ninja / clang / bash / linux / macOs”类型的C ++开发人员,他不习惯像Visual Studio这样的IDE。 I'm not claiming either approach is better or worse, just that I do not know how to work with IDEs. 我并不是说这两种方法是好是坏,只是我不知道如何使用IDE。

I am now in need to develop a C++ project for a Windows-based client who strongly relies on Visual Studio. 现在,我需要为高度依赖Visual Studio的基于Windows的客户端开发C ++项目。

My understanding is that Visual Studio 2017 has built-in support for CMake, Ninja, and Google Test. 我的理解是Visual Studio 2017内置了对CMake,Ninja和Google Test的支持。 However, I am unable to come up with a workflow that would allow me to simply code as usual, commit my code to, say, GitHub, and have my client simply "refresh the repo and rebuild the solution". 但是,我无法提出一个工作流程,该工作流程使我无法像往常一样简单地编写代码,将代码提交到GitHub等,并使我的客户只是“刷新存储库并重建解决方案”。

Here is my question: what is the absolute simplest way in which you would clone a C++/CMake GitHub repository and build it in Visual Studio? 这是我的问题:克隆C ++ / CMake GitHub存储库并在Visual Studio中构建它的绝对最简单的方法是什么?

As a reference, I created the following repository: 作为参考,我创建了以下存储库:

https://github.com/arrieta/visual-studio-cmake-test https://github.com/arrieta/visual-studio-cmake-test

This is how I build it using my normal approach: 这就是我使用常规方法构建它的方式:

$ git clone https://github.com/arrieta/visual-studio-cmake-test.git
$ cd visual-studio-cmake-test
$ mkdir build
$ cd build
$ cmake -G Ninja ../
$ ninja
$ ./app
Welcome to app v0.0.1
Hello, world!

For the life of me, I cannot come up with such simple approach in Visual Studio (not that the tool is bad, it is simply my ignorance). 为了我的一生,我无法在Visual Studio中想出这样简单的方法(不是工具不好,这只是我的无知)。 I create a "Solution", then a "Project", then a "Repo", and I have so many options that I am at a loss. 我创建了一个“解决方案”,然后是“项目”,然后是“回购”,我有太多选择,我不知所措。

Any help is appreciated. 任何帮助表示赞赏。

vre 's Solution vre的解决方案

User vre provided the following approach, which works exactly as intended. vre用户提供了以下方法,该方法完全符合预期。 Here, C:\\> denotes my Windows Developer Command Prompt . 在这里, C:\\>表示我的Windows Developer命令提示符

C:\> git clone https://github.com/arrieta/visual-studio-cmake-test.git
Cloning into 'visual-studio-cmake-test'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 20 (delta 3), reused 20 (delta 3), pack-reused 0Unpacking objects:   5% (1/20)
Unpacking objects: 100% (20/20), done.

C:> cd visual-studio-cmake-test
C:> mkdir build
C:> cd build
C:> cmake -G "Visual Studio 15 2017" ..\
-- The C compiler identification is MSVC 19.13.26129.0
-- The CXX compiler identification is MSVC 19.13.26129.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:[...]/visual-studio-cmake-test/build

C:> cmake --build . --target ALL_BUILD --config Debug -- /nologo /verbosity:minimal /maxcpucount
 [uninteresting output]

C:> cd Debug
C:> app.exe
Welcome to app v0.0.1
Hello, world!

You can target the Visual Studio generator from CMake and then use the build tool mode of CMake. 您可以从CMake定位到Visual Studio生成器,然后使用CMake的生成工具模式。 Eg 例如

cmake -G "Visual Studio 15 2017" ..\\

and

cmake --build . --target ALL_BUILD --config Debug -- /nologo /verbosity:minimal /maxcpucount

all from your build directory. 全部来自您的构建目录。 This uses MSBuild as the native build tool and builds your entire solution from the command line. 这使用MSBuild作为本机构建工具,并从命令行构建整个解决方案。

See this post for further arguments to CMake build tool mode for installing or testing. 有关用于安装或测试的CMake构建工具模式的更多参数,请参见此文章。 CMake + MSVC build tools 2015 - what to do after invoking cmake? CMake + MSVC构建工具2015-调用cmake后该怎么办?

And see the CMake Documentation for the build tool mode https://cmake.org/cmake/help/v3.10/manual/cmake.1.html 并参阅CMake文档以了解构建工具模式https://cmake.org/cmake/help/v3.10/manual/cmake.1.html

Working Example 工作实例

C:\> git clone https://github.com/arrieta/visual-studio-cmake-test.git
Cloning into 'visual-studio-cmake-test'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 20 (delta 3), reused 20 (delta 3), pack-reused 0Unpacking objects:   5% (1/20)
Unpacking objects: 100% (20/20), done.

C:> cd visual-studio-cmake-test
C:> mkdir build
C:> cd build
C:> cmake -G "Visual Studio 15 2017" ..\
-- The C compiler identification is MSVC 19.13.26129.0
-- The CXX compiler identification is MSVC 19.13.26129.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:[...]/visual-studio-cmake-test/build

C:> cmake --build . --target ALL_BUILD --config Debug -- /nologo /verbosity:minimal /maxcpucount
 [uninteresting output]

C:> cd Debug
C:> app.exe
Welcome to app v0.0.1
Hello, world!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM