CMake output/build directory

I'm pretty new to cmake, and read a few tutorials on how to use it, and wrote some complicated 50 lines CMake script in order to make a program for 3 different compilers. This probably concludes all my knowledge in cmake.

Now my problem is that I have some source code, whose folder I don't want to touch/mess when I make the program. I want that all cmake and make output files and folders to go into ../Compile/, so I changed a few variables in my cmake script for that, and it worked for sometime when I did something like this on my laptop:

Compile$ cmake ../src
Compile$ make

Where with that I had a clean output in the folder I'm in right now, which is exactly what I'm looking for.

Now I moved to another computer, and recompiled CMake, and I'm almost back to square one! It always compiles the thing into the src folder where my CMakeLists.txt is located.

The part where I choose the directory in my cmake script is this:

set(dir ${CMAKE_CURRENT_SOURCE_DIR}/../Compile/)
set(LIBRARY_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(CMAKE_BINARY_DIR  ${dir})

And now it always ends with:

-- Build files have been written to: /.../src

Am I missing something?


There's little need to set all the variables you're setting. CMake sets them to reasonable defaults. You should definitely not modify CMAKE_BINARY_DIR or CMAKE_CACHEFILE_DIR. Treat these as read-only.

First remove the existing problematic cache file from the src directory:

cd src
rm CMakeCache.txt
cd ..

Then remove all the set() commands and do:

cd Compile
rm -rf *
cmake ../src

As long as you're outside of the source directory when running CMake, it will not modify the source directory unless your CMakeList explicitly tells it to.

Once you have this working, you can look at where CMake puts things by default, and only if you're not satisfied with the default locations (such as the default value of EXECUTABLE_OUTPUT_PATH), modify only those you need. And try to express them relative to CMAKE_BINARY_DIR, CMAKE_CURRENT_BINARY_DIR, PROJECT_BINARY_DIR etc.

If you look at CMake documentation, you'll see variables partitioned into semantic sections. Except for very special circumstances, you should treat all those listed under "Variables that Provide Information" as read-only inside CMakeLists.

It sounds like you want an out of source build. There are a couple of ways you can create an out of source build.

  1. Do what you were doing, run

    cd /path/to/my/build/folder
    cmake /path/to/my/source/folder

    which will cause cmake to generate a build tree in /path/to/my/build/folder for the source tree in /path/to/my/source/folder.

    Once you've created it, cmake remembers where the source folder is - so you can rerun cmake on the build tree with

    cmake /path/to/my/build/folder

    or even

    cmake .

    if your current directory is already the build folder.

  2. Use some undocumented options to set the source and build folders:

    cmake -B/path/to/my/build/folder -H/path/to/my/source/folder

    which will do exactly the same thing as (1), but without the reliance on the current working directory.

CMake puts all of its outputs in the build tree by default, so unless you are liberally using ${CMAKE_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR} in your cmake files, it shouldn't touch your source tree.

The biggest thing that can go wrong is if you have previously generated a build tree in your source tree (i.e. you have an in source build). Once you've done this the second part of (1) above kicks in, and cmake doesn't make any changes to the source or build locations. Thus, you cannot create an out-of-source build for a source directory with an in-source build. You can fix this fairly easily by removing (at a minimum) CMakeCache.txt from the source directory. There are a few other files (mostly in the CMakeFiles directory) that CMake generates that you should remove as well, but these won't cause cmake to treat the source tree as a build tree.

Since out-of-source builds are often more desirable than in-source builds, you might want to modify your cmake to require out of source builds:

# Ensures that we do an out of source build

     "${CMAKE_BINARY_DIR}" insource)
     "${PARENTDIR}" insourcesubdir)
    IF(insource OR insourcesubdir)
    ENDIF(insource OR insourcesubdir)

    "${CMAKE_PROJECT_NAME} requires an out of source build."

The above macro comes from a commonly used module called MacroOutOfSourceBuild. There are numerous sources for MacroOutOfSourceBuild.cmake on google but I can't seem to find the original and it's short enough to include here in full.

Unfortunately cmake has usually written a few files by the time the macro is invoked, so although it will stop you from actually performing the build you will still need to delete CMakeCache.txt and CMakeFiles.

You may find it useful to set the paths that binaries, shared and static libraries are written to - in which case see how do I make cmake output into a 'bin' dir? (disclaimer, I have the top voted answer on that question...but that's how I know about it).

As of CMake Wiki:

CMAKE_BINARY_DIR if you are building in-source, this is the same as CMAKE_SOURCE_DIR, otherwise this is the top level directory of your build tree

Compare these two variables to determine if out-of-source build was started

Turning my comment into an answer:

In case anyone did what I did, which was start by putting all the build files in the source directory:

cd src
cmake .

cmake will put a bunch of build files and cache files (CMakeCache.txt, CMakeFiles, cmake_install.cmake, etc) in the src dir.

To change to an out of source build, I had to remove all of those files. Then I could do what @Angew recommended in his answer:

mkdir -p src/build
cd src/build
cmake ..

First of all you should not relay on build dir name in your script. line with ../Compile must be changed.

It's because it's up to user where to compile.

Instead of that use one of predefined variables: (look for CMAKE_BINARY_DIR and CMAKE_CURRENT_BINARY_DIR)

Need Your Help

AJAX content in a jQuery UI Tooltip Widget

ajax jquery-ui jquery

There is a new Tooltip Widget in jQuery UI 1.9, whose API docs hint that AJAX content can be displayed in it, but without any further details. I guess I can accomplish something like that with a

How can the Wcf Test Client be used without Visual Studio?

.net visual-studio wcf

When WcfTestClient.exe is copied to another machine by itself, runtime errors prevent you from connecting to a WCF service, for example: