Run CMake executable targets via the cmake command
https://a4z.noexcept.dev/blog/2026/05/23/CMake-Run-Targets.htmlEvery time I use bazel for a while, and come back to CMake, I notice that I miss a cmake --run command. Bazel has a bazel run //target command.
We can not do that with cmake, but something similar.
21
Upvotes
21
u/treddit22 May 23 '26
You can just pass the target name as the command to
add_custom_targetdirectly, no need for$<TARGET_FILE:...>etc. Dependencies are also handled automatically. See https://cmake.org/cmake/help/latest/command/add_custom_target.html for details.4
u/azswcowboy May 23 '26
Sorry, this is what ctest is for, right? Add your executable via add_test build and run ctest. What am I missing?
1
u/CodeMonster2000 May 23 '26
An alternative approach would be to use the CMake file api to discover the location of the built binary and script it outside of CMake, so not having to change CMakeLists.txt/CMakePresets.json at all.
Shameless plug for my PowerShell module that does that: PSCMake
-22
u/Serious-Regular May 23 '26
cmake $BUILD_DIR --target $TARGETlike did you literally not Google this at all?
-7
u/AdOnly69 May 23 '26
We can make make for cmake
-1
u/[deleted] May 23 '26
[removed] — view removed comment