[CMake] choosing specific version of a library with find_package (i.e. choosing macports in /opt/local over system default in /usr)

Rolf Eike Beer eike at sf-mail.de
Thu Jun 7 02:42:08 EDT 2012


Am Mittwoch, 6. Juni 2012, 16:15:52 schrieb Natalie Tasman:
> Andreas, thank you for your help.  Not surprisingly (for someone new
> to cmake) I was getting tripped up with the cmake cache, in part.
> Here's what I've found using cmake 2.8.8:
> 
> 0) One can get tripped up working with the both GUI and the command
> line.  I was manually clearing cache files from the commandline and
> re-generating the cmake project with the GUI.  I didn't realize that
> you had to use the GUI's "delete cache" as well (or just skip the
> GUI.)
> 
> 1) to force an alternate location (such as /opt/local for me), use
>   set(CMAKE_PREFIX_PATH /opt/local ${CMAKE_PREFIX_PATH})
> 
> In this case, cmake ONLY searches the alternate
> 
> 2) to add a "backup" or "fallback" location, use
>   INCLUDE_DIRECTORIES(/opt/local/include)
>   LINK_DIRECTORIES(/opt/local/lib)

This is sortof wrong. You need to specify include_directories() anyway to tell 
the compiler where to search for the libraries. And you for sure should not be 
using link_directories at all as it bypasses the normal link directory 
ordering of CMake.

I assume you want to be able to tell CMake about a location from outside (i.e. 
without touching the CMakeLists.txt anymore) and be sure that CMake finds the 
library in the same place as the header. Try something like the following 
(untested):

find_library(MYLIB foobar HINTS ENV FOOBAR_PREFIX)
 #get's the path
get_filename_component(MYLIB_PREFIX "${MYLIB}" PATH)
#goes one leven upwards, e.g. cuts the "/lib"
get_filename_component(MYLIB_PREFIX "${MYLIB}" PATH)
find_path(MYHDR foo.h HINTS ${MYLIB_PREFIX
          PATH_SUFFIXES include
          NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH
          NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)

To find out what all those options do have a look at "cmake --help-command 
find_library" and "cmake --help-command find_path".

Eike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120607/29ff3b5d/attachment.pgp>


More information about the CMake mailing list