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.
Related
I try to build a CMake (v3.14) project with MSVC 2015. I use the CMake GUI to generate the makefile but when I hit the "Configure" button, I get the following error:
The C compiler identification is MSVC 19.0.24210.0
The CXX compiler identification is MSVC 19.0.24210.0
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Projets/geolibextern/build_msvc/CMakeFiles/CMakeTmp
Run Build Command(s):nmake /nologo cmTC_fa317\fast
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe" -f CMakeFiles\cmTC_fa317.dir\build.make /nologo -L CMakeFiles\cmTC_fa317.dir\build
Building C object CMakeFiles/cmTC_fa317.dir/testCCompiler.c.obj
C:\PROGRA~2\MICROS~2.0\VC\bin\amd64\cl.exe #C:\Users\egrace\AppData\Local\Temp\nm88C7.tmp
testCCompiler.c
Linking C executable cmTC_fa317.exe
"C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_fa317.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~2\MICROS~2.0\VC\bin\amd64\link.exe /nologo #CMakeFiles\cmTC_fa317.dir\objects1.rsp #C:\Users\egrace\AppData\Local\Temp\nm8993.tmp
RC Pass 1: command "rc /foCMakeFiles\cmTC_fa317.dir/manifest.res CMakeFiles\cmTC_fa317.dir/manifest.rc" failed (exit code 0) with the following output:
Le fichier sp‚cifi‚ est introuvableNMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
I also tried (unsuccessfully) to reinstall the Visual C++ build tools or to use a more recent version of CMake.
Indeed, CMake couldn't find the program mt.exe because the Windows SDK folder wasn't in the PATH variable. To solve this problem, I need to execute this command with these arguments:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64 8.1
I'm using Cygwin on Windows 10; but I've recently installed the MSVC 2019 compiler.
I can, of course, write CXX=/path/to/cl.exe (or CC=/path/to/cl.exe if I ignore how MSVC isn't a proper C compiler), but if I try configuring a C or C++ project that way, I get something like:
$ cmake -S . -B build
-- The CXX compiler identification is MSVC 19.29.30133.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/HostX86/x86/cl.exe
-- Check for working CXX compiler: /cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/HostX86/x86/cl.exe - broken
CMake Error at /usr/share/cmake-3.20.0/Modules/CMakeTestCXXCompiler.cmake:59 (message):
The C++ compiler
"/cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/HostX86/x86/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/joeuser/src/myproj/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make.exe -f Makefile cmTC_1b995/fast && /usr/bin/make -f CMakeFiles/cmTC_1b995.dir/build.make CMakeFiles/cmTC_1b995.dir/build
make[1]: Entering directory '/home/joeuser/src/myproj/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_1b995.dir/testCXXCompiler.cxx.o
"/cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/HostX86/x86/cl.exe" -o CMakeFiles/cmTC_1b995.dir/testCXXCompiler.cxx.o -c /home/joeuser/src/myproj/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
cl : Command line warning D9002 : ignoring unknown option '/home/joeuser/src/myproj/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx'
cl : Command line error D8003 : missing source filename
make[1]: *** [CMakeFiles/cmTC_1b995.dir/build.make:78: CMakeFiles/cmTC_1b995.dir/testCXXCompiler.cxx.o] Error 2
make[1]: Leaving directory '/home/joeuser/src/myproj/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:127: cmTC_1b995/fast] Error 2
So, CMake is trying to pass GCC-like command-line options to MSVC. Can I get it to "play nice" and actually be able to use MSVC properly?
On Windows 10, with an installed msys2, which is a variant of mingw, I have an installed cmake with the gcc toolchain and all the standard tools like make.
The version of g++ is 6.2 and make is 4.2.1 Both are in the path.
When I build using cmake:
cmake .
it tries to build using nmake and cl. So somehow because I am on windows cmake is trying to use the visual studio toolchain.
export CMAKE_CXX_COMPILER=g++
does not help. The error is:
-- Building for: NMake Makefiles
-- The C compiler identification is unknown
-- The CXX compiler identification is Clang 3.8.0
-- Check for working C compiler: D:/msys64/mingw64/bin/clang++.exe
CMake Error: Generator: execution of make failed. Make command was: "nmake" "/NOLOGO" "cmTC_87e5f\fast"
-- Check for working C compiler: D:/msys64/mingw64/bin/clang++.exe -- broken
CMake Error at D:/msys64/mingw64/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "D:/msys64/mingw64/bin/clang++.exe" is not able to compile a
simple test program.
It fails with the following output:
Change Dir: D:/git/CSP/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_87e5f\fast"
Generator: execution of make failed. Make command was: "nmake" "/NOLOGO"
"cmTC_87e5f\fast"
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "D:/git/CSP/CMakeFiles/CMakeOutput.log".
See also "D:/git/CSP/CMakeFiles/CMakeError.log".
To use MSYS2 toolchain, you need to specify the build system generator explicitly, i.e.
cmake -G 'MSYS Makefiles' .
Additional note, it's a good practice to separate the application . source file and cmake's generated file, e.g.
mkdir build
cd build
cmake -G 'MSYS Makefiles' ..
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!
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.