Sunday, March 1, 2015

How do CMake determine static or dynamic Boost library?

I was trying to integrate Boost filesystem and system module into CMake in the project, but interestingly, I found out that find_package were not functioning properly:

find_package(Boost 1.54.0 COMPONENTS filesystem system REQUIRED)

I just spot this symptom in this morning. When I am generating a project file from Cmake with this command cmake -G “xxx”, I hit an error as mention below:

CMake Error at D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1182 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.54.0

  Boost include path: C:/Boost/include/boost-1_54

  Could not find the following Boost libraries:

          boost_filesystem
          boost_system

No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.

When I have the debug mode turned on, like this set(Boost_DEBUG 1), I have the following piece captured:
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:890 ] _boost_LIBRARY_SEARCH_DIRS = C:/Boost/lib;C:/Boost/stage/lib;C:/Boost/include/boost-1_54/lib;C:/Boost/include/boost-1_54/../lib;C:/Boost/include/boost-1_54/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1001 ] Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-vc110-mt-1_54;boost_filesystem-vc110-mt;boost_filesystem-mt-1_54;boost_filesystem-mt;boost_filesystem
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1037 ] Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-vc110-mt-gd-1_54;
boost_filesystem-vc110-mt-gd;boost_filesystem-mt-gd-1_54;boost_filesystem-mt-gd;boost_filesystem-mt;boost_filesystem
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1001 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-vc110-mt-1_54;boost_sys
tem-vc110-mt;boost_system-mt-1_54;boost_system-mt;boost_system
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1037 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-vc110-mt-gd-1_54;boost_sy
stem-vc110-mt-gd;boost_system-mt-gd-1_54;boost_system-mt-gd;boost_system-mt;boost_system
-- [ D:/tool/cmake-3.2.0-rc1-win32-x86/share/cmake-3.2/Modules/FindBoost.cmake:1088 ] Boost_FOUND = 1

...
CMake was searching for boost_xxx but I had only libboost_xxx installed in my system. I was wondering how could I direct CMake to take libboost_xxx instead of boost_xxx? Thanks to open source that could allow me to dive into the source code to see what is happening actually. From the clue given above, there is a piece of logic in FindBoost.cmake governing CMake whether a boost library prefix with “lib” or without “lib” should be taken. Here is the piece:
...

set(Boost_LIB_PREFIX "")
if ( WIN32 AND Boost_USE_STATIC_LIBS AND NOT CYGWIN )
  set(Boost_LIB_PREFIX "lib")
endif()

...
Since I'm doing this in DOS prompt, WIN32 and NOT CYGWIN are returning true, but Boost_USE_STATIC_LIBS were returning false. This reminds me that I have something in my code:
...

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ALL_DYN_LINK ON)

...
Clearly, I should ON the Boost_USE_STATIC_LIBS to resolve this problem. And this is interesting to know that a boost static library is prefixed with “lib”.

No comments: