[cmake-developers] targets with the same name as their enclosing directory can't be built when in that directory.

Steve Lorimer steve.lorimer at gmail.com
Mon Sep 5 04:22:38 EDT 2016


I have the following directory structure:

src/
+---- foo/
|     +--- foobar/
+---- bar/

src/CMakeLists adds the foo and bar subdirectories
src/foo/CMakeLists adds the foobar subdirectory
src/foobar/CMakeLists and src/bar/CMakeLists create 2 executables (foobar
and bar)

$ cat src/CMakeLists.txt
cmake_minimum_required (VERSION 3.2.2)
project (cmake_test CXX)

add_subdirectory (foo)
add_subdirectory (bar)

$ cat src/foo/CMakeLists.txt
add_subdirectory (foobar)

$ cat src/foo/foobar/CMakeLists.txt
add_executable(foobar main.cpp)

$ cat src/bar/CMakeLists.txt
add_executable(bar main.cpp)

I generate the makefiles:

$ mkdir build && cd build
$ cmake ..

I can build foobar and bar by specifying "make foobar" and "make bar" from
the build root directory

~src/build/ $ make foobar
[100%] Built target foobar

~src/build/ $ make bar
[100%] Built target bar

However, if I am in the build/foo directory, then nothing happens if I
"make foobar".

~src/build/foo/ $ make foobar

I believe this is because make considers foobar to be a directory it needs
to create, and since it exists, there is nothing to do.

This is different behaviour to when I'm in the build root directory and
"make bar". In this instance "bar" is also a directory, but make behaves
differently in this instance, and builds the bar target. I'm guessing
because in the root makefile "bar" is a phony target, but in the foo
makefile, "foobar" isn't.

If I just type "make" in the foo subdirectory, then all targets defined in
this part of the tree are built correctly.

It is just specifying a target with the same name as a subdirectory from a
location that is deeper than the built root that doesn't work.

How can I enable building from somewhere *inside* the build tree of targets
that have the same name as their enclosing directory?

A github repo with the code presented here is at:
https://github.com/skebanga/nested_cmake

TIA
Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20160905/a69020ee/attachment.html>


More information about the cmake-developers mailing list