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

Natalie Tasman natalie.tasman at insilicos.com
Wed Jun 6 19:15:52 EDT 2012


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)

In this case, cmake first searches default paths and THEN the alternate.


However, and this may be a question for a separate post, it seems to
me that there should be away to tell find_package that it must find
both library and header files from the same prefix.  Specifically,
using method 2) above, cmake finds LibArchive's lib file in /usr, does
not find the header under /usr, and then finds the header under /opt,
and returns success.


-Natalie


On Tue, Jun 5, 2012 at 11:25 PM, Andreas Mohr <andi at lisas.de> wrote:
> Hi,
>
> On Tue, Jun 05, 2012 at 11:11:44AM -0400, cmake-request at cmake.org wrote:
>> Date: Mon, 4 Jun 2012 17:01:34 -0700
>> From: Natalie Tasman <natalie.tasman at insilicos.com>
>> I'm writing a CMakeLists.txt file, and I'm having problems getting
>> find_package to choose libraries under /opt/local rather than
>> /usr/local.  Specifically, I've installed libarchive using macports
>> and CMake cannot find it.  When CMake configuration finds the the
>> library in /usr/lib (/usr/lib/libarchive.dylib) but no corresponding
>> header, it fails, and I would like to find a way to tell CMake to fall
>> back to /opt/local, where a version of the library as well as the
>> archive.h header is installed.
>
> I believe that this is covered by the (now very lengthy)
> description of find_package() in man cmakecommands
> (or, to mention the main one-stop docs solution,
> "cmake --help-command find_package").
> You possibly need to set (well, probably _extend_)
> CMAKE_PREFIX_PATH or CMAKE_FRAMEWORK_PATH, probably in outer shell environment
> (or perhaps only on CMake script side, should work fine, too).
>
> [[background explanation:
> I believe the deeper intention behind these global-switch mechanisms
> is to let the user manually specify which specific architecture
> a build should be done for, i.e. you could have a full toolchain/library set
> for a specific embedded architecture (ARM, MIPS, ..., i.e. NOT
> compatible with the usually activated x86 libraries on your CMake setup),
> and by properly predefining these settings (which for special platforms
> should probably be cleanly carried out in user-custom CMake Platform setup
> files?), CMake knows which library paths to reference (i.e. below which
> PREFIX[es]).]]
>
> I've actually successfully done the same thing on my Mac setup
> (where things provided by e.g. macports/Fink/... need to be referenced additionally),
> and I've simply extended configuration in the script file that
> holds all my main config settings of the project (sorry,
> cannot provide code at the moment).
> (ok, well, in fact I think it's indeed preferable to extend these things
> on script side rather than shell side, since many users would fail
> to *manually* set up shell side config correctly, and after all
> the project config itself knows best what it needs on certain platforms).
>
>> Solutions tried I've so far: Based on other related posts, I've tried
>> things like declaring INCLUDE_DIRECTORIES(/opt/local/include) and
>
> BTW CMake >= 2.8.8 (AFAIR) now finally has a target-specific (i.e.
> non-directory scope) INCLUDE_DIRECTORIES property for specifying these
> things.
>
> HTH,
>
> Andreas Mohr


More information about the CMake mailing list