I am very new to CMake and try to build a minimum CMake file on Windows where I have installed Visual Studio 17 and 19. The C++ code is the bare minimum main.cpp:
#include<iostream>
int main()
{
std::cout << "Hello" << std::endl;
return 0;
}
The CMakeLists.txt file is also the minimum:
cmake_minimum_required(VERSION 3.1)
project(hello)
add_executable(${PROJECT_NAME} main.cpp)
When I run the CMake on the command line, I get this error and I do not know how to resolve it:
C:\SampleProject\build>cmake ./../src
-- Building for: Visual Studio 15 2017
CMake Error at CMakeLists.txt:3 (project):
Failed to run MSBuild command:
MSBuild.exe
to get the value of VCTargetsPath:
The system cannot find the file specified
By default VC++ tools aren't on the PATH, they are added to the PATH by the vcvarsall.bat script.
Run cmake from a "x64 Native Tools Command Prompt for VS 2019" (it's a shortcut to vcvarsall.bat that Visual Studio installs in the Start Menu).
Bonus note:
When not using Visual Studio integration, you might want to consider Ninja as the alternative build system to MSBuild. It's more CMake-friendly because it doesn't require the Debug/Release duality of configuration (which is the source of headache when working with CMake).
Download ninja.exe, add it to the PATH
Use the Ninja CMake generator: cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ..
Enjoy easy and fast builds using the ninja command.
To build a Release version, re-run cmake with -DCMAKE_BUILD_TYPE=Release (so this works very much the same as on other platforms, e.g. Linux).
Ninja also integrates well with the VSCode CMake extension (note: VSCode != VS).
It seems like this should be easy and I've spent hours trying to find this answer online but haven't had any luck.
I can open the CLion folder path in Visual Studio, but that option doesn't give me the usual options to build and start the project. To do that, I need to open CLion as a Project/Solution, but I can't seem to do this.
My professor requires that my C++ code be executable in Visual Studio, but I prefer CLion. So I've done all my work in CLion and want to test that it runs in Visual Studio. How can I import my CLion project?
Thanks!
The link provided describes Visual Studio's CMake integration, which (similar to CLion) will install a version of CMake that Visual Studio will use. These instructions are pretty thorough and should provide everything needed to get your CMake project working in Visual Studio.
Now, you probably have two versions of CMake installed on your machine, one that came with CLion and one that came with Visual Studio. I would recommend installing the latest version of CMake on your machine separately, and configuring both Visual Studio and CLion to use that version instead. However, this is probably getting outside the scope of your immediate problem.
As you follow the Microsoft's instructions for "CMake projects in Visual Studio", you mentioned receiving the error:
1> [CMake] CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.17/Modules/CMakeDetermineSystem.cmake:173 (file):
1> [CMake] file failed to open for writing (No such file or directory):
This looks like a permissions issue, specifically while running CMake within Visual Studio, so be sure you have read/write access to all the files in your project, and the CMake packages in your Visual Studio installation. Hopefully, this doesn't require you re-install Visual Studio in another location on your machine, or run Visual Studio with elevated privileges, but perhaps that is necessary.
If you decide to install CMake separately, the instructions would be the following:
Install the latest CMake on your machine (somewhere you have adequate permissions), and ensure it is available in your Path environment variable. You can verify this by running cmake -version from the command line to see it is the version you just installed.
Using Windows command prompt, navigate to your CMake project directory (containing the top-level CMakeLists.txt file), and run the following:
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A x64 ..
You may run -A Win32 with the cmake command instead if your project is a 32-bit build.
Open the .sln Visual Studio Solution file that was generated in the build directory. Once, the Solution is loaded in Visual Studio, you can build the project (CTRL + SHIFT + B).
I have a module project using cmake with the following configuration:
cmake_minimum_required(VERSION 3.13)
project(app)
set(CMAKE_CXX_STANDARD 11)
add_library(app MODULE src/library.cpp src/library.h)
Once solution generated using cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release, I can find an app.sln solution.
I open it with Visual Studio 2019 and click on the button Local Windows Debugger. I can see also a drop-down menu containing the value x64 and an item Configuration Manager.
Why isn't there an ia32 or x86 option by default? I just can't create a new configuration since the configuration is totally non user friendly.
Anyone has an idea for compile the library for target 32 bits programs?
EDIT
I now compile using cmake .. -G "Visual Studio 16 2019" -DCMAKE_GENERATOR_PLATFORM=x86 -DCMAKE_BUILD_TYPE=Release and the project couldn't be even loaded (cuz of some missing configuration on visual studio, that's what the IDE say)
It appears from the VS 2019 version of the cmake documentation that you should only need to specify:
cmake -G "Visual Studio 16 2019" -A Win32
and I would keep things as simple as possible and let cmake do its job.
Not sure what you mean by "nothing happens" in your last comment. Surely something happened however what happened is not what you expected or wanted.
I am using cmake to generate visual studio project and build it. I am using the following command on my local machine on which visual studio is installed and works fine:
cmake.exe -G "Visual Studio 15 2017 Win64" $(CMAKE_ARGS)
However, I cannot install visual studio on my build server and therefore the above command will fail. During the build I can copy as many files as I want on my build server but I just cannot install anything during the build. Is there anyway to do this without installing VS?
I came across the following cmake argument;
CMAKE_GENERATOR_INSTANCE
which seems to be pointing to the instance of the generator. I was wondering if I could copy the necessary files to my build machine and have this variable point to it in order to generate? Is this how it works? if not, is there a way to generate VS projects using cmake without installing visual studio on the build machine?
How can I compile libzip for Visual Studio 2010?
Edit:
Before starting on the answer provided here, it appears that this may no longer be an issue going by #Thomas Klausner's answer below.
The following should get you a VS10 solution:
If you've not already done so, install CMake
Download and extract zlib to e.g. C:\devel. The download links are about halfway down the homepage. Currently this provides zlib version 1.2.7.
To work around this CMake bug which affects 64-bit Windows only, add
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND MSVC)
set_target_properties(zlibstatic PROPERTIES STATIC_LIBRARY_FLAGS "/machine:x64")
endif()
to the end of C:\devel\zlib-1.2.7\CMakeLists.txt
Download and extract libzip to e.g. C:\devel
In a VS10 command prompt, cd C:\devel\zlib-1.2.7
mkdir build && cd build
cmake .. -G"Visual Studio 10" -DCMAKE_INSTALL_PREFIX="C:\devel\installed\zlib" This sets the install path to C:\devel\installed\zlib rather than the default C:\Program Files\zlib. For 64-bit Windows, use "Visual Studio 10 Win64" as the -G parameter.
msbuild /P:Configuration=Debug INSTALL.vcxproj
msbuild /P:Configuration=Release INSTALL.vcxproj
cd C:\devel\libzip-0.10.1
mkdir build && cd build
cmake .. -G"Visual Studio 10" -DCMAKE_PREFIX_PATH="C:\devel\installed\zlib" Set the path to wherever you installed zlib so that CMake can find zlib's include files and libs. Again, for 64-bit Windows, use "Visual Studio 10 Win64" as the -G parameter.
This should result in C:\devel\libzip-0.10.1\build\libzip.sln. It looks like there are a few POSIX-specific problems in the code, but they should hopefully be fairly easy to resolve (e.g. in zipconf.h #include <inttypes.h> needs replaced with #include <stdint.h>; there are some snprintf calls needing replaced e.g. with _snprintf).
I can't comment, so just in addition to Fraser's answer: In the last days, libzip's latest repository version should compile on VS without additional patches. Please try it out and let the developers know if parts are still missing.
Can't comment on answer above but was trying to get this to work and in the end found that the Output directory under the configuration properties and the comand in debugging.
You can remove ALL_BUILD, ZERO_CHECK, INSTALL and PACKAGE and it will build fine without any of the linking errors or linux specific errors.
Using libzip-1.0.1, zlib-1.2.8, and VS Community 2013.
Added to path:
C:\Program Files (x86)\CMake\bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
The cmake line became:
cmake .. -G"Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX="C:\devel\installed\zlib"
devel\libzip-1.0.1\lib\zip_source_filep.c:189 changed:
mask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
to:
mask = umask(_S_IREAD | _S_IWRITE);
Using
an environment variable %ZLIB_DIR% for the path to zlib-1.2.8,
%LIBZIP_DIR% for the path to libzip-1.0.1
VS 2015 Express Edition, and
the file %LIBZIP_DIR%/lib/zip_source_filep.c patched according to http://hg.nih.at/libzip/rev/80457805a1e7 ,
the process for building zlib and libzip becomes this:
Building zlib
> cd /d %ZLIB_DIR% && md build & cd build
> cmake .. -G"Visual Studio 14 2015 Win64"- DCMAKE_INSTALL_PREFIX="%ZLIB_DIR%"
> msbuild /P:Configuration=Debug INSTALL.vcxproj
> msbuild /P:Configuration=Release INSTALL.vcxproj
Building libzip
> cd /d %LIBZIP_DIR% && md build & cd build
> cmake .. -G"Visual Studio 14 2015 Win64" -DCMAKE_PREFIX_PATH="%ZLIB_DIR%"
> msbuild /P:Configuration=Debug ALL_BUILD.vcxproj
> msbuild /P:Configuration=Release ALL_BUILD.vcxproj
Done!
(So you see, #MikeLischke, CMake does indeed work out-of-the-box sometimes...)
In current zlib version, there is a contrib for this:
zlib-1.2.8\contrib\vstudio\vc10\zlibvc.sln
I got an error on load because one of the configurations wasn't valid on my machine, but a recompile took care of that. I also had to change the project properties>Configuration Properties>Linker>Input>Additional Dependencies for the Debug configuration to change zlibwapi.lib to zlibwapid.lib.
In Visual Studio 2015, Win64:
If building libzip failing with a message like this:
Could NOT find ZLIB (missing: ZLIB_LIBRARY) (found version "1.2.8").
All you have to do is copy the generated 'zlib.dll/zlibd.zll' and 'zlib.lib/zlibd.lib' to the top of the zlib directory (where the .h/.c files are).
The answer given by Christian Severin helped me a lot, but needed some updates for VS2019 and 32-bit:
When building with VS 2019, you must use the -A option, not use "Win64".
When building with VS 2019, the 32-bit archive is "Win32", not x86.
The "Building zlib" section has a typo, it must be Win64" -DCMAKE_..., with a space before the dash
Here is a working example with VS2019 and 32-bit build:
cd /d %ZLIB_DIR% && md build & cd build
cmake .. -G"Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX="%ZLIB_DIR%" -AWin32
msbuild /P:Configuration=Release INSTALL.vcxproj
cd /d %LIBZIP_DIR% && md build & cd build
cmake .. -G"Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX="%ZLIB_DIR%" -AWin32
msbuild /P:Configuration=Release ALL_BUILD.vcxproj