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)
The
FindXXXscripts are supposed to set a variable<Packagename>_FOUNDtoTRUEif the package was found. So in your case, it will setBoost_FOUNDif 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.
Afternote: Since you use the
REQUIREDflag 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 withADD_EXECUTABLEorADD_LIBRARYsince the directories are added to all targets in the same project.