MantisBT - CMake
View Issue Details
0013439CMakeCMakepublic2012-07-25 17:332016-06-10 14:31
Pieter Swinkels 
Philip Lowman 
normalminoralways
closedmoved 
LinuxUbuntu12.04
CMake 2.8.7 
 
0013439: consecutive find_package(Boost ...) statements only find the static lib regardless of Boost_USE_STATIC_LIBS
Consecutive find_package(Boost ...) statements for the same Boost component in the same file CMakeLists.txt but

  - the first time with Boost_USE_STATIC_LIBS to ON and
  - the second time with BOOST_USE_STATIC_LIBS to OFF

always finds the static library.
Try something like this:

set(Boost_USE_STATIC_LIBS ON)

find_package(Boost COMPONENTS unit_test_framework REQUIRED)
if (Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
endif (Boost_FOUND)

# ${BOOST_LIBRARIES} contains the static version of the library

add_executable(my-static-find-package-boost-test main-static.cpp)
target_link_libraries(my-static-find-package-boost-test ${Boost_LIBRARIES})

unset(Boost_LIBRARIES)
message("Boost_LIBRARIES: ${Boost_LIBRARIES}")

set(Boost_USE_STATIC_LIBS OFF)

find_package(Boost COMPONENTS unit_test_framework REQUIRED)
if (Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
endif (Boost_FOUND)

# ${BOOST_LIBRARIES} still contains the static version of the library

add_executable(my-dynamic-find-package-boost-test main-dynamic.cpp)
target_link_libraries(my-dynamic-find-package-boost-test ${Boost_LIBRARIES})
It is not that the dynamic library cannot be found. If I uncomment the first invocation, clear the cache (!) and run cmake, the dynamic library is found.
No tags attached.
Issue History
2012-07-25 17:33Pieter SwinkelsNew Issue
2012-07-30 02:53Rolf Eike BeerNote Added: 0030151
2012-08-06 15:30Pieter SwinkelsNote Added: 0030178
2012-08-10 16:35Pieter SwinkelsNote Added: 0030213
2012-08-18 15:30Philip LowmanStatusnew => assigned
2012-08-18 15:30Philip LowmanAssigned To => Philip Lowman
2012-10-04 02:08Philip LowmanNote Added: 0031176
2016-06-10 14:28Kitware RobotNote Added: 0042096
2016-06-10 14:28Kitware RobotStatusassigned => resolved
2016-06-10 14:28Kitware RobotResolutionopen => moved
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0030151)
Rolf Eike Beer   
2012-07-30 02:53   
Your example can't work this way. You need to clear all the internal cache variables of Boost, otherwise it will just reconstruct BOOST_LIBRARIES from the stuff it already found.
(0030178)
Pieter Swinkels   
2012-08-06 15:30   
Thank you for the reply. I would rather not make any assumptions about the internal cache variables so I better split up my CMake file list.

One thing, I found my approach on http://www.cmake.org/pipermail/cmake/2012-February/049175.html [^] so I was surprised to find it not working. On rereading that email, I see that it already warns me about this situation :(

  "IMO, FindBoost.cmake behaves bad in one regard: It accumulates results from different invocations within the same scope. For this reason, you must intermediately unset the Boost_LIBRARIES variable - and probably all further uncached result variables like Boost_INCLUDE_DIRS also."
(0030213)
Pieter Swinkels   
2012-08-10 16:35   
There is a way to invalidate the Boost cache, see the answer to this question: http://stackoverflow.com/questions/11680168/how-can-i-optionally-link-against-static-or-dynamic-boost-library-using-cmake [^] I quote from that answer "To force the FindBoost CMake module to search for the desired libraries again, you have to clear the cache variables Boost_INCLUDE_DIR and Boost_LIBRARY_DIRS".

In my example, if I add the following statements after "set(Boost_USE_STATIC_LIBS OFF)",

unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)

the shared library is indeed found.
(0031176)
Philip Lowman   
2012-10-04 02:08   
Hmm, did this issue go away when I removed the crazy caching behavior in 2.8.9?
(0042096)
Kitware Robot   
2016-06-10 14:28   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.