How to install CMake packages (configs) globally? - c++

To be honest, I recently started working with find_package in CONFIG-mode... Is there an easy way to install packages globally on Windows 10?
Or I must manually set -DCMAKE_INSTALL_PREFIX and build all stuff by my hand?:
cmake . -Bbuild/debug -DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX="install"
cmake --build build/debug --target install
cmake . -Bbuild/release -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="install"
cmake --build build/release --target install
All I want is - register the package once and forget about it. After the package is registered all commands like find_package(X CONFIG REQUIRED) must work well... Do all paths must be determined manually?

Related

Updating cmake on gitlab runner for macos

I'm using a shell gitlab runner on my macbook. It's task is currently to run a very simple yaml file to build my project using cmake.
build:
before_script:
- git submodule update --init --recursive
- mkdir cmake-build-debug
- cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" .
script:
- cmake --build . --target all -- -j 4
It returns this though: CMake 3.17 or higher is required. You are running version 3.16.3.
When I add - which cmake to my before_script, it returns /usr/bin/cmake
When I run ls /usr/bin | grep cmake in my terminal, it doesn't return anything. When I run brew info cmake it returns the version as cmake: stable 3.19.3
How do I update cmake in my runner? Apparently It can't find brew in the before_script either.
PS: added bonus, if I set the minimum cmake version required to 3.16 it suddenly seems to be unable to link libm
I forgot to add a tag. So it wasn't running on my computer but on a different server.

Need help completing command line installation of {fmt} on Windows 10

I'm a Linux user who's trying to set up a dev environment on Windows. I've cloned the fmt repo and built it the way I'm used to on Linux:
C:\Users\me\Development> git clone https://github.com/fmtlib/fmt.git
C:\Users\me\Development> cd fmt
C:\Users\me\Development> mkdir build
C:\Users\me\Development> cd build
C:\Users\me\Development> cmake ..
C:\Users\me\Development> cmake --build . --config Release
Now I want to install it so that I can include it in projects. Normally I would $ sudo make install but I'm not sure what to do on Windows and the fmt doc page has nothing for Windows installation.
When I did this same set of steps with FLTK there was a variable that I had to set to help me find things:
# CMakeLists.txt
set(FLTK_DIR "Path/to/installation")
find_package(FLTK REQUIRED NO_MODULE)
But it seems to be looking for the installation point, not the build dir. How can I get this to work?
You can run:
cmake --install . [--prefix <install-dir>]
The prefix is optional. On Windows, the default CMAKE_INSTALL_PREFIX is set to:
C:/Program Files (x86)/<project name>
Which in the case fmtlib would be:
C:/Program Files (x86)/FMT
Another option is to use vcpkg on Windows, which will install fmtlib locally in its own vcpkg install prefix. Then you can use it via:
find_package(fmt REQUIRED)
add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE fmt::fmt)
You just have to pass the vcpkg cmake toolchain file to your cmake invocation:
cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake

CMake install target dependencies

I am writing a library that contains library itself and examples and I am using CMake:
cmake_minimum_required(VERSION 3.6)
add_executable (example main.cpp)
install(DIRECTORY include DESTINATION include PATTERN ".DS_Store" EXCLUDE)
When I am running cmake --build . --target install - it compiles example target and makes installation of include directory
I want to exclude building example target and make only include directory installation when building install target and building example if running without any special target:
Here I want example to be NOT built:
cmake --build . --target install
Here I want example to be built:
cmake --build .
How should I change my CMakeLists.txt to make it work as I want?
You cannot exclude single CMake target when installing.
The problem is that 'install' target may depends only from 'all' (default) target.
While you may remove 'install' -> 'all' dependency (by setting CMAKE_SKIP_INSTALL_ALL_DEPENDENCY variable), you may not add another dependency for 'install'.
So, before installing
cmake --build . --target install
either performs
cmake --build .
or doesn't build anything (even library, which you want to build in any case).

How to set GoogleTest variables GTEST_LIBRARY GTEST_INCLUDE_DIR and GTEST_MAIN_LIBRARY to CMake on Windows?

There is a project that was developed for linux environment. Now I am trying to build this on windows using CMake.
I keep trying to build the project and always get this error:
CMake Error at C:/Program Files(x86)/CMake/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
Call Stack (most recent call first):
C:/Program Files(x86)/CMake/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files(x86)/CMake/share/cmake-3.3/Modules/FindGTest.cmake:204 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
C:/Users/awy9/git/cmake_modules/modules/SigeoGTest.cmake:21 (find_package)
C:/Users/awy9/git/cmake_modules/modules/SigeoInit.cmake:29 (include)
CMakeLists.txt:12 (include)
How can I set these variables to work on this project now on Windows?
Honestly It's not the best idea to set hardcoded variables like you've done with GTEST_LIBRARY and GTEST_MAIN_LIBRARY.
I had the exact same problem. I was using gtest 1.7.0 on windows and getting the same error.
Turns out "GTest" should be "gtest" (not capitalized).
So the code in my CMakeLists.txt became:
find_package(gtest REQUIRED)
Note that I was using CMake 3.9
EDIT
I would highly recommend following this guide now:
http://crascit.com/2015/07/25/cmake-gtest/
It is by far the best method I have found to use unbuilt gtest and gmock in your cmake project.
UPDATE
#Tsyvarev helped me solving the first problem and I quote him:
This is standard CMake message, when requested package(GTest in your case) is not found. You should install GTest before configuring your project. Or, if you already have installed it into non-standard location, pass this location via GTEST_ROOT variable: cmake -DGTEST_ROOT= ...
As alternative for the passing variable to cmake, you can set environment variable: set GTEST_ROOT=
This solved the problem with the value of GTEST_INCLUDE_DIR, but I still had these errors:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.3/Modules/FindPackageH andleStandardArgs.cmake:148 (message): Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)
SOLUTION
I figured that on Windows environment some values are different from what the manuals say.
After compiling Gtest, I got two libs: gtest.lib and gtest_main.lib
Then, I set these filepath variables:
GTEST_LIBRARY=C:\Users\awy9\Softwares\Gtest\gtest-1.7.0\Debug\gtest.lib
GTEST_MAIN_LIBRARY=C:\Users\awy9\Softwares\Gtest\gtest-1.7.0\Debug\gtest_main.lib
Now everything is working!
Thank you
I've come up with a similar issue when building my project through GitHub Actions on Windows platform.
Even if I export the CMAKE_PREFIX_PATH to enable the find_package(GTest REQUIRED) to properly find the framework library (Windows is a bit nasty BTW), it continues to fail with Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY). Note that it correctly fetches the include directory.
After digging a little bit deeper into the issue I realized that on Windows the command cmake --build . --target install without any --config options, builds the library in Debug configuration, contrary to Linux and macOS.
If you are then building your project in Release, e.g. cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release --target install, the find_package(GTest REQUIRED) won't find any GTest library because it searches for the nonexistent Release build.
In this case a better solution is to install GTest in Release mode w.r.t. hardcoding the library paths to fetch the Debug\ directory builds.
I attach also the GitHub Action steps which may be helpful for someone:
steps:
- name: Clone GTest
shell: bash
working-directory: ${{runner.workspace}}
run: git clone https://github.com/google/googletest.git
- name: Create GTest Build Environment
shell: bash
working-directory: ${{runner.workspace}}/googletest
run: cmake -E make_directory build
- name: Configure GTest CMake
shell: bash
working-directory: ${{runner.workspace}}/googletest/build
run: cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_FLAGS=-std=c++11
- name: Install GTest
shell: bash
working-directory: ${{runner.workspace}}/googletest/build
run: cmake --build . --config $BUILD_TYPE --target install || sudo cmake --build . --config $BUILD_TYPE --target install
- name: Export CMAKE_PREFIX_PATH on Windows
shell: bash
if: runner.os == 'Windows'
run: echo '::set-env name=CMAKE_PREFIX_PATH::C:/Program Files (x86)/googletest-distribution'
- name: Checkout
uses: actions/checkout#v2
- ... your project build steps

how to use make and make install in mac

I have a question related to cmake and make in Windows. As we already knows that in windows we can use
cmake -G"Visual Studio 12" ..
cmake --build . --target INSTALL --config Release
to compile and install the target using command lines. In linux, we can use
cmake -DCMAKE_BUILD_TYPE=Release ..
make
make install
to do the same job. Then how about mac? In mac, I tried to use:
cmake -DCMAKE_OSX_ARCHITECTURES=x86_64 ../ -G Xcode
make
make install
but failed. Any ideas? Thanks.
You can use cmake --build on any platform as it
abstracts a native build tool's command-line interface