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 2.8.11.2, 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(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(LIBRARY_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dir})
set(CMAKE_BUILD_FILES_DIRECTORY ${dir})
set(CMAKE_BUILD_DIRECTORY ${dir})
set(CMAKE_BINARY_DIR ${dir})
SET(EXECUTABLE_OUTPUT_PATH ${dir})
SET(LIBRARY_OUTPUT_PATH ${dir}lib)
SET(CMAKE_CACHEFILE_DIR ${dir})
And now it always ends with:
-- Build files have been written to: /.../src
Am I missing something?
It sounds like you want an out of source build. There are a couple of ways you can create an out of source build.
Do what you were doing, run
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
or even
if your current directory is already the build folder.
Use some undocumented options to set the source and build folders:
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 theCMakeFiles
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:
The above macro comes from a commonly used module called
MacroOutOfSourceBuild
. There are numerous sources forMacroOutOfSourceBuild.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
andCMakeFiles
.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:
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:
cmake will put a bunch of build files and cache files (
CMakeCache.txt
,CMakeFiles
,cmake_install.cmake
, etc) in thesrc
dir.To change to an out of source build, I had to remove all of those files. Then I could do what @Agnew recommended in his answer:
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
orCMAKE_CACHEFILE_DIR
. Treat these as read-only.First remove the existing problematic cache file from the src directory:
Then remove all the
set()
commands and do: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 toCMAKE_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.
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: http://www.cmake.org/Wiki/CMake_Useful_Variables (look for CMAKE_BINARY_DIR and CMAKE_CURRENT_BINARY_DIR)