68

I feel almost silly for asking this but I couldn't find anything on this...

Suppose I have a cmake project containing a number of targets (libraries, executables, external targets, etc). How do I list them using the cmake command line interface?

I want a list of things that are valid to substitute for $target in the following command line:

cmake . && cmake --build . --target $target

Lot's of bonus points for a solution that uses neither grep nor find nor python nor perl nor... you get the idea.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Holger
  • 1,648
  • 1
  • 16
  • 26

4 Answers4

83

For Makefile generator build environments you could use

cmake --build . --target help

And there is the graphical output solution (example found here):

cmake --graphviz=test.graph 
dotty test.graph

See also Generating Dependency Graphs with CMake and CMake Graphviz Output Cleaner.

If you don't have dotty installed, you can still make the target dependencies visible with enabling GLOBAL_DEPENDS_DEBUG_MODE in your CMakeLists.txt:

set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)

The disadavantage here is that you can't trigger it from the command line. It will always show on stderr when generating the make environment.

References

Florian
  • 39,996
  • 9
  • 133
  • 149
  • 1
    Well, it would be nice to have something that is agnostic of the underlying generator but make works for me. I don't really like the graphical output solution since it requires dotty which I consider worse than perl, grep and bash because it doesn't come in the default installation of any system I use. I'll still award the points for the make-based solution. – Holger Jun 14 '15 at 01:05
  • Thanks. I've added information for using the `GLOBAL_DEPENDS_DEBUG_MODE` global property. That will work without `dotty` and even without the `--graphviz` command line option. And - because I was also missing this in CMake before - I'm planning to post a feature request for a `--list-all-targets` command line option on CMake's bug tracker. I'll keep you updated here if I get positive feedback. – Florian Jun 14 '15 at 20:12
  • 12
    Note that ``cmake --build . --target help`` is basically the same as ``make help`` – Ignitor Jul 26 '16 at 08:50
  • 4
    @Ignitor Yes, it's an abstraction so you don't need to know the `make` call syntax or the [make program's path](https://cmake.org/cmake/help/latest/variable/CMAKE_MAKE_PROGRAM.html). I used it here because it also works with other "makefile generators" like `nmake`, `gmake`, `ninja`, ... – Florian Jul 26 '16 at 09:08
  • @Florian: Your second link seems to be down :-( – Michael Kopp Jul 17 '18 at 11:25
  • @MichaelKopp Thanks for the hint. I've updated the link to point to the internet archive (since this particular block post was removed). – Florian Jul 22 '18 at 19:22
  • 1
    Any generator-independent options? Looking for msbuild, specifically. – MHebes Jan 27 '21 at 01:21
  • Can you tweak your answer so as to clarify in which directory one needs to run which command? Or instead, to specify the source and the build directories? – einpoklum Aug 15 '21 at 07:34
  • My testing shows that with cmake v 3.21, `cmake -B --target help` works for at least the generators: `Unix Makefiles`, `Ninja`, `Ninja Multi-Config` and possibly others. – jeffmcc Jun 27 '23 at 17:55
18

I think you might be looking for the command make help.

When I run make help (after running cmake ..) I get the following output:

The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
etc

You could also read the Makefile that cmake auto-generates for you.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
  • this also (like the accepted answer) only works when the cmake generator is set to makefiles, which is the default on linux, but for osx or windows cmake uses xcode or visual studio as default generator respectively. And on linux we tend to prefer ninja, it does multi-threading builds by default and can be used to generate compile_commands.json from. – Emile Vrijdags Oct 15 '21 at 08:42
1

We may get all targets of the generated Makefile, as @Florian and @Olivia Stork answered.

However, people may just looking for explicitly declared targets in CMakeLists.txt . Targets like "all" and "clean" may not be what people is interested in.

Thus, they can simply query "Built target" in the output of make.

i.e.

cd ~/work/my_project
mkdir build && cd build && cmake ..
make -j4 > log.txt 2>&1
grep 'Built target' log.txt | awk '{print $4}'
ChrisZZ
  • 1,521
  • 2
  • 17
  • 24
1

Answer by @Florian is correct. Just to give some context to it, the command

cmake --build . --target help is assuming your build directory is at current directory, as indicated by the "dot".

If you set your build directory to another directory other than current directory, let say, /build, then you should specify it as cmake --build build --target help.

Alternatively, you can also

cd build
make help
ken
  • 13,869
  • 6
  • 42
  • 36