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
View all comments
Show parent comment
1
u/throw_cpp_account May 25 '26
This really is something else. I don't know what your problem is. I posted a comment showing that Bazel is more user-friendly than CMake (and later edited it to show that many other build systems are also more user-friendly in this way) on a post pointing out another way in which Bazel is more user-friendly. This gets me vitriol and labeled a zealot dumping on non-existent issues? Buddy, you don't know anything about me. You don't know my opinion on Bazel. You certainly don't even know if I use Bazel (incidentally, I do not).
Are you going to call me a Rust zealot if I point out that, of course,
cargo testvscargo test --releasejust does the right thing and puts things indebugandreleasedirectories and builds everything as you'd expect (I don't use Rust either).After all of that, do you even understand what it is I'm complaining about? Because you posted this:
Alright. If it is indeed so far out of left field then it should actually be very easy to prove me wrong. If it's actually possible to do this (i.e. have a single command to run a test, or set of tests, that also builds it/them if necessary) in CMake, I'd love to hear it, because I actually do use CMake and it would make my life, and my colleagues' lives, easier. So I'd appreciate if you'd enlighten me.
But the patronizing bullshit ain't it.
cmake --buildtakes a build directory, not a test target, so that obviously does not build and run my tests. So do better.Given a simple CMakeLists.txt
Doing something like
cmake --build build --target testwill run the test, but won't build it (which best case will fail because it was never built, but worst case is stale). That's the "non-existent issue" that plenty of other build systems actually handle correctly.cmake --build build --target test_foois invalid.cmake --build build --target foowill of course build the binary properly, but the point is to do this in one step, not two. I can alsoctest --test-dir build -R '^test_foo$', but that also only runs the test, it doesn't build it.I don't know what you actually meant to suggest, maybe you just meant that building
allworks. Which yes,allworks. Butallis far more than necessary, and especially isn't helpful if I'm iterating on a single test.