[CMake] CMake performs search for includes/libs in non-default compiler search paths.

Andreas Pakulat apaku at gmx.de
Thu Jan 27 16:31:55 EST 2011


On 27.01.11 20:08:06, Óscar Fuentes wrote:
> Michael Wild <themiwi at gmail.com> writes:
> > IMHO you're the one trying to jump through hoops that aren't even there...
> 
> Okay, so if I have this:
> 
> check_include_file(foo.h HAVE_FOO_H)
> 
> and it succeeds, I have no guarantees that a program such as this:
> 
> #include "foo.h"
> 
> will compile, becasue foo.h may be found on a directory not included on
> the compiler's default search list.

Actually, thats wrong. If you look at the check_include_file macro
you'll notice that it uses try_compile. That in turn (as documented in
man cmake) creates a cmakelists.txt which only adds include dirs set via
the INCLUDE_DIRECTORIES variable passed into the internal cmake call and
otherwise just uses a simple add_executable() call for the source. 

The convenience paths are only added to things such as find_path,
find_library etc. They are _not_ added to compiler invocations
automatically.

Hence the above check_include_file would fail if foo.h is in
/usr/local/include and your compiler does not search there by default.

So you'd need to either set CMAKE_REQUIRED_INCLUDES or use find_path and
then add the director to include_directories().

> We end having something like /usr/local/include (or /opt/local/include
> or /opt/pkg/include etc) on the search list.
> 
> We now test for the presence of some third party library ("thelib"). We
> have an user-settable variable for using just in case we want to use an
> specific install, and we use it:
> 
> cmake -DTHELIB_IS_HERE=/home/oscar/thelib .
> 
> The search mechanism for thelib looks into that directory first and
> succeeds, as it should. We add the corresponding directory:
> 
> include_directories( ${THELIB_INCLUDE_PATH} )
> 
> But there is another install of thelib on /usr/local, and that install
> is the one that will be used by our build because we end having this
> search list:
> 
> /usr/local/include /home/oscar/thelib/include

This has nothing to do with the default paths of the compiler, all you
need to do with this is fix the order of include's of your target or
source files. You'd run into compile problems (or compiling against the
wrong lib version) no matter wether cmake searches in /usr/local/include
for foo.h or not - unless you actually wanted cmake to _not_ find the
foo.h header at all.

If you want to ensure that /home/oscar/thelib/include is being searched
before /usr/local/include then use include_directories with its BEFORE
parameter so it ends up at the front of the list. This is the same thing
if you'd create the compiler command yourself in a shell.

Andreas

-- 
You will be called upon to help a friend in trouble.


More information about the CMake mailing list