Visual Studio 2017 - how to set the installation directory - cmake

I need to build a library on Windows. I generate a sln file using cmake:
cmake -G "Visual Studio 15 2017 Win64" CMakeLists.txt
Then I open sln file in Visual Studio and I build the solution. There is INSTALL.vcxproj. It looks like I should run it because "include" directory for that lib is not separated. How I can set the installation directory in Visual Studio ?

You need to set the cache variable CMAKE_INSTALL_PREFIX to the directory you want to install to via the install target when setting up the project.
Furthermore you should specify the directory containing the CMakeLists.txt file, not the file itself (in source builds are usually not a good idea anyways.
cmake -G "Visual Studio 15 2017" -A x64 -D "CMAKE_INSTALL_PREFIX:PATH=C:/Program Files/some_dir" source_dir
Note that the path for installing from a package can be set seperately, if you're planing to use the PACK target/cpack; Use CPACK_PACKAGING_INSTALL_PREFIX for this purpose.
Btw: Usually the files from the include directory are available regardless of whether you install the target or not via INTERFACE_INCLUDE_DIRECTORIES target property of the target, so if you link the library using target_link_libraries, the include directories should be available; At least that's the case, if the library is properly set up.

Related

CMake with Build Tools for Visual Studio 2019 (Just the CLI not the IDE)

I am trying to build my code with Cmake and Visual Studio 2019 Build Tools.
Note that I'm not using the IDE only the Build Tools CLI.
When I try to generate my project like this:
cmake -G "Visual Studio 16 2019" -S . -B .
CMake returns this:
Generator
Visual Studio 16 2019
could not find specified instance of Visual Studio:
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community
How can I make CMake to look for the Build Tools location and not for the IDE location?
I haven't used Visual Studio in years, but according to CMake's documentation on the Visual Studio 16 2019 generator, you can use a CMake variable or an environment variable to control which instance you use.
VS 2019 supports multiple installations on the same machine. The CMAKE_GENERATOR_INSTANCE variable may be set as a cache entry containing the absolute path to a Visual Studio instance. If the value is not specified explicitly by the user or a toolchain file, CMake queries the Visual Studio Installer to locate VS instances, chooses one, and sets the variable as a cache entry to hold the value persistently.
When CMake first chooses an instance, if the VS160COMNTOOLS environment variable is set and points to the Common7/Tools directory within one of the instances, that instance will be used. Otherwise, if more than one instance is installed we do not define which one is chosen by default.
Make sure you have all of the necessary individual components selected when installing the build tools.
Make sure CMake can find the tools (e.g. add the paths to your Path variable).
As a proof of concept for my case, I added the following to my user Path variable for lib.exe, cl.exe, link.exe, etc...:
C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64
And the following path for mt.exe and rc.exe:
C:/Program Files (x86)/Windows Kits/10/bin/10.0.18362.0/x64

Is it possible to build cmake projects directly using MSBuildTools

Currently we are planning to use VS2017 with a cmake project. Inside Visual Studio this works quite like a charm.
Now want to run our builds as part of CI on a dedicated build master running MSBuildTools.
Is it possible to directly run the build using the msbuild command, without creating solution files with cmake? Optimally, I would even use the CMakeSettings.json used from VS2017.
Use the build-tool-mode of CMake for this. It uses the underlying default build tool which is MSBuild for Visual Studio Generators.
From the build directory call:
cmake --build . --target ALL_BUILD --config Release -- /nologo /verbosity:minimal /maxcpucount
and you get a fast, nearly quiet build. To install use INSTALL target, for running your tests if configured use RUN_TESTS target.
Is it possible to directly run the build using the msbuild command, without creating solution files with cmake?
Is it possible to directly run the build using the msbuild command, without creating solution files with cmake?
As far as I know, CMake produces Visual Studio Projects and Solutions seamlessly. So you can produce projects/solutions.
The only tricky part is to remember to make any changes in the cmake files, rather than from within Visual Studio.
In particular, each CMake project will create a Visual Studio solution (.sln file), while all of the CMake targets belonging to that CMake project will appear as Visual Studio projects within the corresponding solution.
CMake Visual Studio
project <-> Solution (.sln)
Target <-> Project (.vcxproj)
You can check cmake-and-visual-studio for more details.
Since MSbuild can build both solution files and project files, so you could also call msbuild INSTALL.vcxproj
Is it possible to even use the CMakeSettings.json used in VS2017?
The answer is yes, check the blog for details.
If your CMake project requires additional settings to configure the
CMake cache correctly, you can customize these settings by creating a
CMakeSettings.json file in the same folder with the root
CMakeLists.txt. In this file you can specify as many CMake
configurations as you need – you will be able to switch between them
at any time.
You can create the CMakeSettings.json file by selecting the Project >
Edit Settings > path-to-CMakeLists (configuration-name) menu entry.

Google GMOCK build libgmock.a under windows

I try to build the gmock library from google under windows, avialable on github from here:
https://github.com/google/googletest/tree/master/googlemock
I tried to use cmake in the cygwin console, but I could not build it.
cmake C:\Users\Username\Downloads\googlemock-master\googlemock-master\googlemock
"CMake Error: The source directory "C:UsersSETDownloadsgooglemock-mastergooglemock-mastergooglemock" does not exist.
Specify --help for usage, or press the help button on the CMake GUI."
Then I installed visual studio 2017 and opened the gmock.sln file, but also this build failed.
"Error C1083: "gtest/internal/gtest-linked_ptr.h": No such file or directory gmock C:\Users\Username\Downloads\googlemock-master\googlemock-master\googlemock\include\gmock\internal\gmock-port.h"
Does anyone have an idea how I could build this library under windows 10?
Edit: Ok, for cmake the path needs to have /../ and not ..\, but i still don't get which path I need to include in cmake
Very simple:
Under the googlemock folder, create a new folder, named build (for example);
cd build && cmake ..
Basically, you're creating a new folder for the build (preferably inside the project tree, but not necessary), cd into it, and run cmake <dir>, where <dir> is the path to CMakeLists.txt, which contains the recipe for generating the build.
That's it. Now you'll have a generated gmock.sln, which you could build with Visual Studio.
For CMake to generate Visual Studio projects, you should have the Visual Studio binaries and Windows SDK reachable from your PATH.
Finally, you need to specify a generator, using CMake's -G parameter, for telling CMake which Visual Studio version you'd like projects to be generated for.
Example of putting this together:
set PATH="C:\Program Files\Microsoft Visual Studio 14.0\VC\bin";"c:\Program Files\Windows Kits\8.1\bin\x86";%PATH%
cd build
cmake -G "Visual Studio 14 2015" ..
For additional instructions, you may refer to googletest github page:
https://github.com/google/googletest/blob/master/googletest/README.md#using-cmake

CMake creates x64 or Win32 directory outside its Build directory

In CMake, I usually create a 'Build' directory at the root level of my source tree. I then CD into this directory, and do:
CMake .. -G "..."
In the past, all the CMake-related files would be created within this 'Build' directory. However, I now notice (after switching to new version of CMake, 3.7) that there is an 'x64' or 'Win32' file created in the root of the source tree when I build the project in Visual Studio.
How can I make it so that CMake creates these 'x64' or 'Win32' directories within the 'Build' directory and not in the parent directory, which is the root of the codebase?
Are you using Visual Studio 2017? From the tags you have chosen, I think you are. There is actually a bug with Visual Studio 2017. You can read more details about it here since it has been first reported as a CMake bug.
In a nutshell, it seems to occur only when using "Rebuild" from the IDE. It results in a x64 (or Win32) folder being created in the root folder along with intermediate log files and such from ALL_BUILD and ZERO_CHECK projects.
The issue has been closed by CMake 18 hours ago as it has been confirmed that this bug was fixed in Visual Studio 15.3.0.
Hope this helps.

How to install library from add_subdirectory in current directory

I'm using LuaDist's LuaJIT and I'm trying to add it to my CMake project via add_subdirectory. It seems to build the solution, but it doesn't actually make the libs. How do I tell the sub_directory to also make and install? Do I need to edit that project's CMake, or can I do it from my CMake project (preferred)?
Additionally, when I build and install LuaJIT seperately it installs to C:\Program Files (x86). I don't want that when I build it from my project. I want it to install to my projects \lib\LuaJIT directory (or basically build in place).
# lua
message( STATUS "Building Lua")
include_directories(${PROJECT_SOURCE_DIR}/libs/luajit/src)
add_subdirectory(${PROJECT_SOURCE_DIR}/libs/luajit)