Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if find_package found the package (boost)

Tags:

I want to not add boost.cxx if cmake find_package found no boost installed. Does find_package return something that I can wrap in condition to compile boost.cxx or not. Here is my current cmake file:

add_executable (complex complex.cxx lexer.cxx boost.cxx ../../src/lili.cxx ../../src/lilu.cxx)  # Make sure the compiler can find all include files include_directories (../../src)  include_directories (.)  # Make sure the linker can find all needed libraries # rt: clock_gettime() target_link_libraries(complex rt)  # Install example application install (TARGETS complex          RUNTIME DESTINATION bin)  IF(UNIX)     find_package(Boost COMPONENTS system filesystem REQUIRED)      ## Compiler flags     if(CMAKE_COMPILER_IS_GNUCXX)         set(CMAKE_CXX_FLAGS "-O2")         set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")     endif()      target_link_libraries(complex        ${Boost_FILESYSTEM_LIBRARY}       ${Boost_SYSTEM_LIBRARY}       #${PROTOBUF_LIBRARY}     ) ENDIF(UNIX) 
like image 544
cnd Avatar asked Apr 19 '12 11:04

cnd


People also ask

How does CMake find boost?

You can use find_package to search for available boost libraries. It defers searching for Boost to FindBoost. cmake, which is default installed with CMake. Upon finding Boost, the find_package() call will have filled many variables (check the reference for FindBoost.

How does CMake find package?

CMake searches for a file called Find<package>. cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. It is responsible for finding the package, checking the version, and producing any needed messages.

Where does CMake Find_package look?

CMake ships with its own set of built-in find_package scripts, and their location is in the default CMAKE_MODULE_PATH.

Where is CMake path in Windows?

CMake maintains a list of package information in the Windows registry under HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\ . Packages build from source can register there using the export command. Other projects build later on the same machine will then be able to find that package without additional configuration.


1 Answers

The FindXXX scripts are supposed to set a variable <Packagename>_FOUND to TRUEif the package was found. So in your case, it will set Boost_FOUND if boost was found.

When compiling your Boost.cxx, I assume that you will need Boost headers as well, so you should adjust your include directories as well.*

look for Boost before creating your executable. Furhtermore, you need to set your include directories before adding the executable.

IF(UNIX)     find_package(Boost COMPONENTS system filesystem REQUIRED)     # IF( Boost_FOUND ) # checking this variable isnt even necessary, since you added                         # REQUIRED to your call to FIND_PACKAGE         SET( BOOST_SRC_FILES boost.cxx )         INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} ) # you could move this down as well                                                      # as ${Boost_INCLUDE_DIRS} will be                                                      # empty if Boost was not found     # ENDIF() ENDIF()  add_executable (complex complex.cxx lexer.cxx ${BOOST_SRC_FILES} ../../src/lili.cxx ../../src/lilu.cxx)  # Make sure the compiler can find all include files include_directories (../../src)  include_directories (.) # INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} ) # alternative location to                                                 # add include dirs, see above  # Make sure the linker can find all needed libraries # rt: clock_gettime() target_link_libraries(complex rt)  # Install example application install (TARGETS complex          RUNTIME DESTINATION bin)  IF(UNIX)      ## Compiler flags     if(CMAKE_COMPILER_IS_GNUCXX)         set(CMAKE_CXX_FLAGS "-O2")         set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")     endif()      target_link_libraries(complex        ${Boost_FILESYSTEM_LIBRARY}       ${Boost_SYSTEM_LIBRARY}       #${PROTOBUF_LIBRARY}     ) ENDIF(UNIX) 

Afternote: Since you use the REQUIRED flag when looking for Boost (since you only need it on Unix platform) it is even sufficient to use the optional-source-files-in-a-variable trick.

(*) Thanks to your question, I just found out that it doesn't matter whether include_directories(...) is called before or after creating the target with ADD_EXECUTABLE or ADD_LIBRARY since the directories are added to all targets in the same project.

like image 110
Johannes S. Avatar answered Oct 06 '22 19:10

Johannes S.