Home > Mobile >  How to install a cpp library using cmake on Windows x64?
How to install a cpp library using cmake on Windows x64?

Time:08-21

I'm using CLion with MinGW-GCC on the Windows-x64 platform - This is the background of the problem.

I was trying to install gtest before. But a lot of confusion arose in the middle.

First time I ran those commands(in googletest-release-1.12.1\) according to the instructions of googletest-release-1.12.1\googletest\README.md:

mkdir build
cd build
cmake ..

But I got error messages like:

CMake Error at CMakeLists.txt:51 (project):
  Failed to run MSBuild command:

    C:/Windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe

  to get the value of VCTargetsPath:

Then I changed my last command to

cmake -G "MinGW Makefiles" ..

because I use make provided by MinGW. I don't know whether it's right but, it ran properly.

then I called

make
make install

make ran smoothly. But when I ran make install, I got these messages:

Install the project...
-- Install configuration: ""
-- Installing: C:/Program Files (x86)/googletest-distribution/include
CMake Error at googlemock/cmake_install.cmake:41 (file):
  file INSTALL cannot make directory "C:/Program Files
  (x86)/googletest-distribution/include": No such file or directory.
Call Stack (most recent call first):
  cmake_install.cmake:42 (include)


make: *** [Makefile:109: install] Error 1

I have no idea at all this time. So I changed my way. According to this answer, I copied the whole library into my project and edited CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.23)
project(gtest_study)

set(CMAKE_CXX_STANDARD 20)

add_subdirectory(googletest-release-1.12.1)
include_directories(googletest-release-1.12.1/googletest/include)
include_directories(googletest-release-1.12.1/googlemock/include)

add_executable(gtest_study main.cpp)
target_link_libraries(gtest_study gtest gtest_main)
target_link_libraries(gtest_study gmock gmock_main)

So my questions are:

  1. Is there any difference between the two which build it using make and cmake metioned firstly, and just use commands like include_directories and target_link_libraries in CMakeLists.txt? (maybe like .h and .dll file? Or just completely the same? I don't know)
  2. When I use make install to install a library on Windows, what should I do in particular? Specify some directory (I don't know which one) or what?
  3. Although in my system environment I use MinGW-makefile, in CLion which the libraries are eventually used, I use ninja as the generator for CMake (it just comes with CLion, not installed for the system). Do I have to specify it and how? (-G "Ninja"doesn't work in my native env)

CodePudding user response:

The difference between

cmake ..

and

cmake -G "MinGW Makefiles" ..

Is the choice of generator: The former uses the default generator, the latter uses the generator you specified. (cmake --help should put a * next to the default generator.)

Based on the error message I assume this is a visual studio generator and you may not be able to run that one properly from within a MinGW terminal.

In the latter case the default install directory seems to be based on the target OS (Windows) but does not seem to incorporate the fact that you're running from a MinGW terminal where the default install path (C:/Program Files (x86)/googletest-distribution) is not valid.

You could try to fix this by providing it during cmake configuration (passing -D 'CMAKE_INSTALL_PREFIX=/c/Program Files (x86)/googletest-distribution' before the source dir) or by providing the install directory during the installation.

The following process should allow you to install the lib. I'm using my preferred way of building here, i.e. not using build system dependent commands, but using cmake to run the build/install commands. I assume the working directory to be the root directory of the gtest sources:

cmake -G "MinGW Makefiles" -S . -B build
cmake --build build
cmake --install build --prefix '/c/Program Files (x86)/googletest-distribution'

The last command needs to be run with admin privileges, the first 2 I don't recommend running as admin. You could instead install to a directory where you do have the permissions to create directories even without admin privileges.


The difference between using the process described above and using add_subdirectory is that the former results in a installation on the system which can be used via find_package and the google test libs won't be rebuilt for every project where you do this.

...
project(gtest_study)

...
# you may need to pass the install location via -D CMAKE_PREFIX_PATH=<install_location> during configuration for this to work
find_package(GTest REQUIRED)

target_link_libraries(gtest_study PRIVATE GTest::gtest_main GTest::gmock)

The latter builds the google test project as part of your own project build and for every project where you use this approach a seperate version of the google test libs is built. Note: there should be no need to specify the include dirs yourself, since this kind of information is attached to the cmake target and gets applied to the linking target automatically:

#include_directories(googletest-release-1.12.1/googletest/include)
#include_directories(googletest-release-1.12.1/googlemock/include)

add_executable(gtest_study main.cpp)
target_link_libraries(gtest_study PRIVATE gtest_main gmock)

As for 3.: The CMake generator used for building GTest should be independent of the generator of the project using it. The thing that's important is that the compilers used by the build systems are compatible. I cannot go into detail about this, since I've never used CLion and therefore have too little knowlege about the compilers used by it. (Personally I'm working with Visual Studio on Windows.)

  • Related