简体   繁体   中英

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. I'm not claiming either approach is better or worse, just that I do not know how to work with IDEs.

I am now in need to develop a C++ project for a Windows-based client who strongly relies on Visual Studio.

My understanding is that Visual Studio 2017 has built-in support for CMake, Ninja, and 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".

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?

As a reference, I created the following repository:

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

User vre provided the following approach, which works exactly as intended. Here, C:\\> denotes my Windows Developer Command Prompt .

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

See this post for further arguments to CMake build tool mode for installing or testing. CMake + MSVC build tools 2015 - what to do after invoking cmake?

And see the CMake Documentation for the build tool mode 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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