CMake install target dependencies - c++

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).

Related

cmake --target source directory does not exist

I am compiling a rather big C++ project with cmake.
The project has several targets defined in the CMakeLists.txt of the various subdirectories
I can create a folder somewhere and use cmake to prepare compilation of the targets, and compile any of the target :
cmake ..
make -j8 cytosim
And this works.
However, if I call cmake with a target :
cmake .. --target cytosim
I get the error
CMake Error: The source directory ".../build/cytosim" does not exist.
I thought target was supposed to be a module/library/executable target... What am I missing ?
CMake --target argument is usable in CMake Build Mode (--build).
Try cmake --build .. --target cytosim

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

How do I change the name of the executable that I'm installing using CPack?

I do apologize in advanced if this sounds like something trivial. I am new to using CMake and CPack.
I am currently trying to build my own compiler as a side project, and I want to test out how CPack will install my project.
Here is my CMakeLists.txt file that I have at the root of my project folder:
cmake_minimum_required(VERSION 3.15)
project(Simple-C-Compiler VERSION 0.01)
set(CMAKE_CXX_STANDARD 20)
set(COMPILER_VERSION ${PROJECT_VERSION})
add_library(include INTERFACE)
target_include_directories(include INTERFACE include/)
add_subdirectory(lib)
add_subdirectory(phases)
add_subdirectory(compiler)
add_subdirectory(tests)
target_link_libraries(compiler lexer)
target_link_libraries(tester lexer)
add_compile_options(-Wall)
install(TARGETS compiler DESTINATION bin)
set(CPACK_PACKAGE_EXECUTABLES "compiler" "Simple-C")
include(CPack)
When I try to install my compiler, by doing:
mkdir build
cd build
cmake ../
make install
I get the following output:
[ 22%] Built target lib
[ 55%] Built target lexer
[ 77%] Built target compiler
[100%] Built target tester
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/compiler
CPack installed my compiler as "compiler" not "Simple-C". I want the name of the executable being installed to be "Simple-C". How do I go about doing that in my CMakeLists.txt file?
You can change the name of a target with the following command:
set_target_properties(compiler PROPERTIES OUTPUT_NAME Simple-C)
This must be called after the add_subdirectory(compiler)
As a side note, the commands you mentionned did not invoke cpack. In order to invoke cpack, you would need to run the cpack command.
You can use the RENAME option of CMake install. See https://cmake.org/cmake/help/v3.13/command/install.html
In short
install(TARGETS compiler DESTINATION bin RENAME Simple-C)

cmake to make multiple executable files

Currently, I have CMakeLists and hoge.cpp in a directory, and running CMakeLists and make command generates hoge executable file.
Now I added hoge2.cpp and want to be able to generate two different hoge and hoge2 executable files by running CMakeLists and "make hoge" and "make hoge2" commands.
How can I do this?
Create two build targets in your CMakeLists.txt file.
add_executable( hoge hoge.cpp )
add_executable( hoge2 hoge2.cpp )
Then you can run (from same directory as your CMakeLists.txt file,
cmake --build . --target hoge
For the other build target use
cmake --build . --target hoge2
You can refer to the CMake documentation or the man pages for more information. Try running CMake with just the --build flag to get help.

How to install CMake packages (configs) globally?

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?