[Cmake] cmake 1.8 INCLUDE_DIRECTORIES out-of-source problem

Brad King brad.king at kitware.com
Thu, 22 Jan 2004 17:27:16 -0500 (EST)


On Wed, 21 Jan 2004, Zennard Sun wrote:

> Hi,
>     I have a question regarding CMake 1.8.3.  I recently asked about how
> to use relative pathnames with INCLUDE.  I was told to specify pathnames
> relative to the source directory when using INCLUDE in 1.8.3.  However,
> specifying paths from the source directory doesn't seem to work when
> using INCLUDE_DIRECTORIES (in version 1.6.7, I would specify relative
> pathnames for both INCLUDE and INCLUDE_DIRECTORIES from the build
> directory).  For example, I have the following setup:
>
> folder/src
> (where the source files are as well as CMakeLists.txt)
>
> folder/obj/linux
> (the build directory where the out-of-source build goes)
>
> folder/helper.txt
> (a file I want to include into my CMakeLists.txt)
>
> folder/otherstuff.h
> (a file I want to include in my compilation)
>
> In CMake 1.8.3, I would have:
>
> SET(file_path .. CACHE PATH "Offset to file location")
> INCLUDE(${file_path}/helper.txt)
> INCLUDE_DIRECTORIES(${file_path})
>
> but when I run cmake ../src from the folder/obj/linux directory, the
> Makefile gives me "-I.." rather than "-I../.." in the compile line.
> Should I use INCLUDE_DIRECTORIES differently than INCLUDE?  Thanks.

Yes, even though it seems counter-intuitive.  Here is what happens:

The INCLUDE command specifies information that is processed by CMake
itself and affects its own execution.  These paths are expanded in a way
that makes sense for CMake.

The INCLUDE_DIRECTORIES command specifies information that is passed to
the compiler.  These paths are expanded in a way that makes sense for the
compiler.  In this case, the paths are relative to the directory from
which the compiler runs.  The same thing occurs for the LINK_DIRECTORIES
command and the linker.

-Brad