[CMake] newbie q - where do I put what in which CMakeLists file? out of source build

Michael Hertling mhertling at online.de
Thu Mar 10 08:48:19 EST 2011


On 03/10/2011 03:11 AM, Pierre Abbat wrote:
> On Wednesday 09 March 2011 13:09:39 Michael Hertling wrote:
>> Could you boil down your project to a minimal but complete example
>> which demonstrates the issues with the header not being found and
>> the files not being placed properly and post it here?
> 
> Here's an example of it compiling correctly, but making a package wrong: 
> minimal.tar.gz .
> 
> -bash-4.1$ cd build
> -bash-4.1$ cmake ../src
> -- The C compiler identification is GNU
> -- The CXX compiler identification is GNU
> <snip>
> -bash-4.1$ make
> Scanning dependencies of target readmidi
> [100%] Building CXX object CMakeFiles/readmidi.dir/midi.o
> Linking CXX executable readmidi
> [100%] Built target readmidi
> -bash-4.1$ ./readmidi
> Header, 6 bytes
> Track, 19 bytes
> Track, 655 bytes
> Track, 675 bytes
> Track, 1491 bytes
> Track, 1334 bytes
> -bash-4.1$ make package_source
> <snip>
> bash-4.1$ cd /tmp
> -bash-4.1$ tar xvzf ~/minimal/build/minimal-0.1.1-Source.tar.gz
> x minimal-0.1.1-Source/config.h.in
> x minimal-0.1.1-Source/midi.h
> x minimal-0.1.1-Source/CMakeLists.txt~
> x minimal-0.1.1-Source/midi.cpp
> x minimal-0.1.1-Source/CMakeLists.txt
> x minimal-0.1.1-Source/midi.cpp~
> 
> For minimal2, follow the same steps except start with "cmake ..". The program 
> doesn't compile; the package is rooted correctly, but contains contents of 
> the build directory as well as the src directory.
> 
> Pierre

The question w.r.t. your first minimal example should be answered by
David and Eric in the meantime. The reason why your minimal2 example
doesn't compile is that you mentioned INCLUDE_DIRECTORIES() *after*
ADD_SUBDIRECTORY() in your top-level CMakeLists.txt, so it does not
take effect for the compilation of sources in the src directory. In
general, the order of commands in CMakeLists.txt files does matter.

Furthermore, you have a persistent file in the build directory; you
should not do so. Besides that you don't really do an out-of-source
build in such a manner, a build directory is usually temporary, i.e.
the users create and enter it, configure/build/test/install/package
the project from within in and finally throw it away. Instead, you
should place the MIDI files in their own directory, refer to them
by, say, ${CMAKE_SOURCE_DIR}/midi and reserve the build directory
for the build process only.

Moreover, while it's common to create a subdirectory of the project's
top-level directory for building, it's also common to use another one
for that purpose, e.g. /tmp, /var/tmp, ~/tmp, /dev/shm etc., and the
users are completely free to choose, so you should not insist on the
usage of ${CMAKE_SOURCE_DIR}/build. Additionally, ${CMAKE_SOURCE_DIR}
might be read-only, and build directories that are descendants of the
source directory could have some pitfalls - as you have seen. Finally,
with single-configuration generators, notably the Makefile ones, you
can build only one configuration per build directory, i.e. *either*
RELEASE *or* DEBUG *or* <your-config-here> etc., so it's perfectly
legal to have multiple build trees side by side at the same time.

Regards,

Michael


More information about the CMake mailing list