r/cpp • • May 23 '26

Run CMake executable targets via the cmake command

https://a4z.noexcept.dev/blog/2026/05/23/CMake-Run-Targets.html

Every 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

41 comments sorted by

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 test vs cargo test --release just does the right thing and puts things in debug and release directories 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:

This is just so far out of left field this isn't worth debunking. The short version though, is if you're using cmake --build <test target>, unless you're doing something I haven't seen before, it will build if necessary and run your tests.

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 --build takes 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

add_executable(foo foo.cxx)
add_test(NAME test_foo COMMAND foo)

Doing something like cmake --build build --target test will 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_foo is invalid. cmake --build build --target foo will of course build the binary properly, but the point is to do this in one step, not two. I can also ctest --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 all works. Which yes, all works. But all is far more than necessary, and especially isn't helpful if I'm iterating on a single test.

2

u/13steinj May 25 '26 edited May 25 '26

Doing something like cmake --build build --target test will 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.

https://gist.github.com/13steinj/09b1cb4206689ca14e7922c09b3f465a#file-proof-log

Adapted from here. I don't like how verbose this is though, and generally write a function that does this for me. Key warning: if you get the "link" groups wrong (PUBLIC/PRIVATE/INTERFACE) you'll either error out or get the wrong behavior (which is expected... but both the "wrong behavior" case and

You can use Build and Test mode at the top level for a one-shot command if you like, or as shown here to have the DAG wired correctly. Note that the makefile generator is a known mess here, I can't tell you if it has the correct behavior. The VS generators in theory should have the right behavior, but I'm not a Windows/msbuild guy so I can't confirm.


There are multiple ways to map individual tests to their own target, but I prefer build-and-test mode (e.g. I think with -R works?) at the toplevel for this, and use an alias to make the command shorter. You can generally create a bunch of subdirs / subproject()s and that will give you names for groups of tests that you can --build.

Again, I completely agree that the defaults / default behavior of add_test absolutely sucks here, even worse that you need to read "Mastering CMake" to learn how to do it right. I think the history here is that ctest and cmake started off as separate projects, ctest for use and integration with cdash, and eventually they merged codebases (because they use the same scripting language, but different builtins / "standard library" modules) but for historical reasons didn't add the automatic dag-wiring (because the cmake add_test builtin and the ctest add_test builtin are actively different in the source code).

I would take my bet that if one were to follow their contribution guidelines, and have an LLM slop together:

  • a policy
  • docs
  • a deprecation/removal timeline

They'd accept it, same goes for automatic mapping of add_test to individual targets (rather than a singular test target per subdir/subproject).


E: Automatic grouped targets are made for add_subdirectory, I could have sworn the same is true for subproject()s but I can't get that to work with 4.3.2.

I've slopped it up myself, I'd link it here but I think that's against sub rules. I'm sure you can find it on my github. It might take some iteration to actually get merged, I don't know how this policy interacts with all the different existing modes of ctest.

1

u/throw_cpp_account May 27 '26

Wow. What in the world. Ok, well I've never seen anything remotely like that before. Not in any blog, examples, nothing. Thank you for the demo.

Again, I completely agree that the defaults / default behavior of add_test absolutely sucks here

Putting it mildly. I don't understand how this isn't just the default. Or, for that matter, if it were the default, I can't really imagine wanting to opt-out.

1

u/13steinj May 27 '26

I think this isn't the default because, as I mentioned, history and backwards compatibility, and then no one cared enough to make a pull request with a cmake policy.

You have to take into consideration that while they may have similar names and now share a repo, cmake and ctest are actually completely separate tools that happen to share the same language (maybe not even that) with different builtins and a different stdlib.

Under the hood cmake add_test calls a builtin, that adds to the generation of an automatic CTestTestfile.cmake basically escaping / prettifying the name and just calling add_test again. ctest is what is intended to interpret this file, so this time add_test runs something different.

If you check my github (again not linking because against sub rules) I've created 3 branches that

  • slop this up (should probably change to changing the default test launcher instead)
  • slop this up + a build target per test (instead of per subdir)
  • the above + wire the build dag internally rather than rely on build-and-test, but only because this might lead to some better behavior with other ctest features).

Even after getting merged I imagine it will take 2 years to get all the edge cases shook out and the policys set to NEW by default.


But also, shit like this (and various bullshit in every build system) is why companies [should] hire dedicated build engineers. It's its own complex ball that deserves a dedicated resource. Thank your nearest build engineer/random dev who cares about a good clean build.