[CMake] Finding Boost for Windows and Linux.

Brad King brad.king at kitware.com
Mon Jun 19 11:53:15 EDT 2006


Andrew Maclean wrote:
> Bill, If you think it is generally OK and useful to people, tell me what 
> improvements are needed before it would be eligible for inclusion in the 
> modules directory of CMake.

This will definately be a useful contribution.

> I have had a go at writing a FindBoost.cmake file (attached). It tries 
> to find where the boost includes are using "educated guesses" and then 
> tries to determine the path to the library files. The problem with 
> windows is that there is no "default" library path.
>  
> Is there a better way of doing this for both windows and linux? I have 
> searched the web but most examples are just for linux and only seem to 
> do part of the job.

Does Boost suggest that any environment variables be set for its 
location?  Does it set any registry entries on Windows?

> The rationale for my solution is outlined below.
>  
> I think my solution is a bit rough around the edges but pointing in the 
> right direction. For example:
> If I have a path like /usr/local/include/boost-1_33_1/boost
> How can I get the /usr/local part so I can append /lib to it?

Using GET_FILENAME_COMPONENT is fine, but you may not need to strip as 
much as you are now.

> The problem is that I could have a paths like:
>  /user/local/include/boost
>  /user/include/boost-1_33_1/boost
>  /user/local/include/boost
>  C:\boost\include\boost
>  C:\boost\include\boost\boost-1_33_1\boost
>  
> My problem is that I want to set up a BOOST_INCLUDE_PATH (easy, just 
> look for a file like config.hpp).
> The hard part is determining the library path it is either:
> ${BOOST_INCLUDE_PATH}/../../../lib
> or
> ${BOOST_INCLUDE_PATH}/../../lib

What determines during boost installation which of these paths is used? 
  I'm guessing it is whether the version number was included in the 
install directory..

>  You can see from the attached code that I used a series of IF ( EXISTS ...
> 
> Is this the best way to do this?
> # Usage:
> # In your CMakeLists.txt file do something like this:
> # ...
> # # Boost
> # INCLUDE(FindBoost.cmake)

This should be

FIND_PACKAGE(Boost)

The module should also pay attention to the variable 
Boost_FIND_REQUIRED.  If it is set and boost is not found it should exit 
with a MESSAGE(FATAL_ERROR ...).  The variable is set by the 
FIND_PACKAGE command when it loads the module if it is called like this:

FIND_PACKAGE(Boost REQUIRED)

For finding the include path, look at the PATH_SUFFIXES option of the 
FIND_PATH command.  You should be able to find the headers like this:

FIND_PATH(BOOST_INCLUDE_DIR
           NAMES boost/config.hpp
           PATH_SUFFIXES boost-1_33_1 ...)

Then you will not need to strip the boost/ part of the path, and the 
sub-path boost-1_33_1 will be searched underneath every other search 
path specified.  Then you can use code like

IF("${BOOST_INCLUDE_DIR}" MATCHES "boost-[0-9]+_[0-9]+")

to test how many levels need to be stripped to find the PREFIX/lib 
directory.

-Brad


More information about the CMake mailing list