[CMake] Help request for hierarchical directory example

Eric Noulard eric.noulard at gmail.com
Fri Oct 18 07:56:57 EDT 2019


Le ven. 18 oct. 2019 à 12:53, David Aldrich <david.aldrich.ntml at gmail.com>
a écrit :

> Hi
>
>
>
> I'm learning how to use hierarchical directories in CMake and am trying to
> get an example to work that I saw on YouTube. The example isn't doing what
> I expect so I would be grateful for some help in understanding why.
>
>
>
> I am running CMake 3.10.2 on Ubuntu 18.04 (Microsoft WSL) and using make.
>
>
>
> I have a project called 'cmake-good' that should build library
> 'libsay-hello.a' and executable 'cmake-good'.
>
>
>
> Here's the directory tree (excluding CMake artifacts which I don't think I
> need to show):
>
>
>
> ├── CMakeLists.txt
>
> ├── build
>
> │   ├── CMakeCache.txt
>
> │   ├── CMakeFiles
>
> │   ├── Makefile
>
> │   ├── cmake_install.cmake
>
> │   ├── hello-exe
>
> │   │   ├── Makefile
>
> │   │   ├── cmake-good
>
> │   └── say-hello
>
> │       ├── Makefile
>
> │       └── libsay-hello.a
>
> ├── hello-exe
>
> │   ├── CMakeLists.txt
>
> │   └── main.cpp
>
> ├── say-hello
>
>     ├── CMakeLists.txt
>
>     └── src
>
>         └── say-hello
>
>             ├── hello.cpp
>
>             └── hello.hpp
>
>
>
> Here are the CMakeLists.txt files:
>
>
>
> 1) Top level CMakeLists.txt:
>
>
>
> cmake_minimum_required(VERSION 3.10)
>
> project(MyProject VERSION 1.0.0)
>
> add_subdirectory(say-hello)
>
> add_subdirectory(hello-exe)
>
>
>
> 2) hello_exe/CMakeLists.txt:
>
>
>
> add_executable(cmake-good main.cpp )
>
> target_link_libraries(cmake-good PRIVATE say-hello)
>
>
>
> 3) say-hello/CMakeLists.txt:
>
>
>
> add_library(
>
>     say-hello
>
>     src/say-hello/hello.hpp
>
>     src/say-hello/hello.cpp
>
> )
>
> target_include_directories(say-hello PUBLIC
> "${CMAKE_CURRENT_SOURCE_DIR}/src")
>
>
>
> My problem is that I expect to see:
>
>
>
>     hello-exe/cmake-good
>
>     say-hello/libsay-hello.a
>
>
>
> but I see:
>
>
>
>     build\hello-exe\cmake-good
>
>     build\say-hello\libsay-hello.a
>
>
>
> Why is that?
>

Because build/ is your build directory and you apparently did an
out-of-source build (which is good practice)
see :
https://gitlab.kitware.com/cmake/community/wikis/FAQ#out-of-source-build-trees

You should have done something like:

cd cmake-good/build
cmake ..
make

In this case everything the build is generating (CMake artefact, build
artefact etc...) gets written build/
the directory hierarchy in build will have the same structure as your
source tree.

This is an expected behaviour.



-- 
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191018/900a51b6/attachment.html>


More information about the CMake mailing list