Visual Studio to build C++/CMake project hosted in GitHub - c++

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. E.g
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!

Related

make: *** makefile. stop

I am new to cmake, and when I compile the makefile using "make -j10" after cmake, it failed and turned out "make: *** makefile. stop." I am really confused. Thanks in advance for precious suggestions!
G:\code\c++\assignment2\code\test>mkdir build
G:\code\c++\assignment2\code\test>cd build
G:\code\c++\assignment2\code\test\build>cmake ..
-- Building for: Visual Studio 16 2019
-- The C compiler identification is MSVC 19.28.29912.0
-- The CXX compiler identification is MSVC 19.28.29912.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: G:/vs/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: G:/vs/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Found Boost: G:/boost_1_75_0/ (found version "1.75.0") found components: timer chrono
-- Configuring done
-- Generating done
-- Build files have been written to: G:/code/c++/assignment2/code/test/build
G:\code\c++\assignment2\code\test\build>make -j10
make: *** makefile. stop.
Run cmake --build . -j 10 under your build directory.
You didn't build makefiles, you built Visual Studio project files, as can be seen from this line:
Building for: Visual Studio 16 2019
To instruct CMake to generate makefiles instead, use cmake's -G flag:
cmake .. -G "Unix Makefiles"
Or if you didn't intend to build makefiles, you can compile using the generated build system (in this case Visual Studio) as shown in aleck099's answer.

VS2017 developer command prompt only builds to x86 - need x64

I'm trying to build some C++ libraries using cmake using the VS2017 developer command prompt. I need to build them for a Release x64 setup, however, the command prompt only seems to build them to x86.
First, I run this command (note the build type Release64):
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release64 -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- The C compiler identification is MSVC 19.16.27039.0
-- The CXX compiler identification is MSVC 19.16.27039.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
--
-- 3.13.0.0
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: C:/test_game_server/protobuf/cmake/build/release64
Then I run nmake, but I see this error:
...
[ 49%] Linking CXX static library gmock_main.lib
[ 49%] Built target gmock_main
[ 50%] Generating C:/test_game_server/protobuf/src/google/protobuf/any_test.pb.cc
google/protobuf/any_test.pb.cc: while trying to create directory C:/test_game_server/protobuf/src/google: No error
NMAKE : fatal error U1077: '.\protoc.exe' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
This error still occurred even after I used Vcvarsall.bat to set a 64-bit hosted build architecture:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.22
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
It is worth noting that there are no errors when I compile the project with the option Release instead of Release64.
I looked for the x64 native or cross-tool developer command prompts, but I don't have any associated with VS2017 (I do have one for VS2010 but the C++ version is out of date). Compiling in my standard Windows command prompt is not an option.
How can I force this project to compile to x64 instead of x86?
If you do not require using NMake, you could use the Visual Studio's build system by first generating for 64 bit (note the Win64 part):
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release64 -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=../../../../install ../..
And then building via cmake's build command:
cmake --build <directory> --config Release64
CMake will then generate and execute whatever command is needed for building using Visual Studio itself on the command line.
You aren't using CMAKE_BUILD_TYPE properly.
https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
Unless you have defined a configuration besides the default ones (Debug, Release, RelWithDebInfo, MinSizeRel) what you are doing makes no sense.
Configurations have nothing to do with the architecture you are compiling for.
The architecture you are compiling for has to do with tool-chains and compiler options.
======================================
2nd
NMake Makefiles are ridiculously slow. Use Ninja or Visual Studio instead.
======================================
3rd
Here are instructions for building for x64/x86 using visual studio (I use 2019, but it shouldn't matter)
cmake -G "Visual Studio 16 2019" -A Win32 -S path_to_source -B "build32"
cmake --build build32 --config Release
cmake -G "Visual Studio 16 2019" -A x64 -S path_to_source -B "build64"
cmake --build build64 --config Release
======================================
4th
If you wondering how to configure/build for single configuration generators like ninja/nmake-builds please open up another question. Because it's a loaded question.

cmake mingw-w64: strange error when trying to build

I have installed MinGW-w64 and built a simple Hello World Program. But the second time I run cmake --build . a strange error occurs.
When the Path variable is set to C:/MinGW/bin everything is fine and works. But when I set the Path to C:/msys64/mingw64/bin this happens:
PS C:\repos\hellovs\build> cmake ../ -G Ninja
-- The CXX compiler identification is GNU 10.1.0
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.exe
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.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:/repos/hellovs/build
PS C:\repos\hellovs\build> cmake --build ./
[2/2] Linking CXX executable HelloVS.exe
PS C:\repos\hellovs\build> cmake --build ./
ninja: error: FindFirstFileExA(c/:/msys64/mingw64/include/c++/10.1.0): ╤шэЄръёшўхёър  ю°шсър т шьхэш Їрщыр, шьхэш яряъш шыш ьхЄъх Єюьр.
HelloVS.exe compiles normally the first time the build command is run.
Apparanteley Ninja tryies to use an invalid path. Any idea about how to fix this?
Seems like invalid dependencies have been generated:
PS C:\repos\hellovs\build> ninja -t recompact
PS C:\repos\hellovs\build> ninja -t deps
CMakeFiles/HelloVS.dir/main.cpp.obj: #deps 136, deps mtime 6109023486207704 (VALID)
../main.cpp
C/:/msys64/mingw64/include/c++/10.1.0/iostream
C/:/msys64/mingw64/include/c++/10.1.0/x86_64-w64-mingw32/bits/c++config.h
C/:/msys64/mingw64/include/c++/10.1.0/x86_64-w64-mingw32/bits/os_defines.h
C/:/msys64/mingw64/include/c++/10.1.0/x86_64-w64-mingw32/bits/cpu_defines.h
C/:/msys64/mingw64/include/c++/10.1.0/pstl/pstl_config.h
C/:/msys64/mingw64/include/c++/10.1.0/ostream
...
It's a GCC bug.
MSYS2 maintainers decided to not wait for the upstream and patched it.
Run pacman -Syuu from MSYS2 shell to update your packages.

Simple cmake project, build with nmake on windows has no debug symbols

I'm trying to lear using CMAKE to create builds. I've been experimenting but can't get debugging to work on my resulting projects. To test this, I've setup a basic hello world project like this:
SimpleProject
--Include
--Main.cpp
--CMakeLists.txt
This is the contents of Main.cpp.
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
std::getchar();
return 0;
}
and this is the content of CMakeLists.txt:
cmake_minimum_required(VERSION 3.15.3)
project(CleanProject LANGUAGES CXX C)
add_executable(CleanProject Include/Main.cpp)
I now run cmake with the following command, in the developer console:
D:\Development\SimpleProject\Build>cmake -DCMAKE_BUILD_TYPE=Debug ..
-- Building for: NMake Makefiles
-- The CXX compiler identification is MSVC 19.23.28105.4
-- The C compiler identification is MSVC 19.23.28105.4
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual
Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual
Studio/2019/Community/VC/Tools/MSVC/14.23.28105/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
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx86/x86/cl.exe --
works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Development/SimpleProject/Build
Lastly, I build using nmake
Microsoft (R) Program Maintenance Utility Version 14.23.28105.4
Copyright (C) Microsoft Corporation. All rights reserved.
Scanning dependencies of target CleanProject
[ 50%] Building CXX object CMakeFiles/CleanProject.dir/Include/Main.cpp.obj
Main.cpp
[100%] Linking CXX executable CleanProject.exe
[100%] Built target CleanProject
If I now try to debug this program, be it in an IDE like eclipse or by using gdb from the command line, I'll get the no debugging symbols found error as seen below.
D:\Development\SimpleProject\Build>C:\MinGW\bin\gdb.exe CleanProject.exe
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/...
Reading symbols from D:\Development\SimpleProject\Build\CleanProject.exe...(no debugging symbols found)...done.
I can't seem to find a fix for this. I've tried adding several different vars to my cmakelist file or as extra options for cmake with no success.
Any help would be greatly appreciated as this is the only roadblock left as of now. My bigger projects also build successfully but without debugging it's kinda hard to continue.
Thanks!
Reading your CMake output:
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx86/x86/cl.exe -- works
It seems like you are using the Visual Studio compiler.
The way it compiles is incompatible with the GDB debugger, you'll have to use the MSVC debugger instead, which can be run within the visual studio debugger.

make: No targets provided near line 28 (CMake)

I am having newbie problems with CMake. The error I get is make: No targets provided near line 28
My setup is as follows:
Developer Command Prompt for VS2015
Microsoft Windows [Version 6.3.9600]
cmake version 2.8.11.2
make version1.1 (Jan 28 2007)
The steps I have followed are listed on CMake Tutorial | CMake in the first section A Basic Starting Point (Step1).
Quoting from the website...
The most basic project is an executable built from source code files. For simple projects a two line CMakeLists.txt file is all that is required. This will be the starting point for our tutorial. The CMakeLists.txt file looks like:
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)
Note that this example uses lower case commands in the CMakeLists.txt file. Upper, lower, and mixed case commands are supported by CMake. The source code for tutorial.cxx will compute the square root of a number and the first version of it is very simple, as follows:
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
Commands
c:\HW>cd c:\hw\tutorial
c:\HW\tutorial>cd build
c:\HW\tutorial\build>cmake ../src
-- Building for: NMake Makefiles
-- The C compiler identification is MSVC 19.0.23026.0
-- The CXX compiler identification is MSVC 19.0.23026.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio
14.0/VC/bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio
14.0/VC/bin/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 Studi
o 14.0/VC/bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studi
o 14.0/VC/bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/HW/tutorial/build
c:\HW\tutorial\build>make
make: No targets provided near line 28
Lines 26 to 31 of Makefile, line 28 emphasized:
!IF "$(OS)" == "Windows_NT"
NULL=
!ELSE
NULL=nul
!ENDIF
SHELL = cmd.exe
(Incidentally when I ran GNU make (just to see what would happen) I got
makefile:28: *** missing separator. Stop.)
You're using CMake's generator NMake Makefiles, which generates a buildsystem for nmake. So run nmake instead of make to do the build.