'What happened to CMake's --target argument?

With CMake version 3.19.2 I can use --target argument to build specific targets, instead of all. For example --target tests to build tests. However, with CMake 3.22.1 I'm getting an error like this:

CMake Error: Unknown argument --target
CMake Error: Run 'cmake --help' for all supported options.

You can see the manual of CMake here: https://cmake.org/cmake/help/latest/manual/cmake.1.html
(There is a drop-down list for version selection)

It describes the --target argument, and It doesn't seem any different from what it was earlier. Nonetheless, after switching from 3.19.2 to 3.22.1 it doesn't let me use --target.

@EDIT thank you for your feedback, here's what I use:

cmake -G Ninja -DCROSS_COMPILER_PREFIX=<some_prefix> -Dsomeothervariables=1 --target tests $directory_with_cmake_project

It works with 3.19.2, but executing the same thing with cmake 3.22.1 causes the error. I expect that the order of providing -G Ninja, variables, target directory and --target matters, but I haven't managed to get it to work in any order I could think of.



Solution 1:[1]

CMake prints this message, if you haven't specified a directory to build by passing --build <some dir> first. (The --target option only if mentioned in this usage version of the cmake command line tool, see the documentation.)

Wrong

cmake --target foo
CMake Error: Unknown argument --target
CMake Error: Run 'cmake --help' for all supported options.

Correct
(assuming in the subdirectory build of the current working directory contains a configured cmake project with a target foo).

cmake --build build --target foo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fabian