两种方式,使用系统默认安装路径,通常是/usr/lib/x86_64-linux-gnu/
:
find_package(Boost REQUIRED regex date_time system filesystem thread graph)
include_directories(${BOOST_INCLUDE_DIRS})
message("boost lib: ${Boost_LIBRARIES}")
message("boost inc:${Boost_INCLUDE_DIR}")
add_executable(use_boost use_boost.cpp)
target_link_libraries(use_boost
${Boost_LIBRARIES}
)
如果将Boost安装在本地目录中或选择本地安装而不是系统安装,则可以通过以下方式进行:
set( BOOST_ROOT "/home/xy/boost_install/lib/" CACHE PATH "Boost library path" )
set( Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost" )
find_package(Boost REQUIRED regex date_time system filesystem thread graph)
include_directories(${BOOST_INCLUDE_DIRS})
message("boost lib: ${Boost_LIBRARIES}, inc:${Boost_INCLUDE_DIR}")
add_executable(use_boost use_boost.cpp)
target_link_libraries(use_boost
${Boost_LIBRARIES}
)
请注意,上面的目录/home/xy/boost_install/lib/
是我安装Boost的位置:
xy@xy:~/boost_install/lib$ ll -th
total 16K
drwxrwxr-x 2 xy xy 4.0K May 28 19:23 lib/
drwxrwxr-x 3 xy xy 4.0K May 28 19:22 include/
xy@xy:~/boost_install/lib$ ll -th lib/
total 57M
drwxrwxr-x 2 xy xy 4.0K May 28 19:23 ./
-rw-rw-r-- 1 xy xy 2.3M May 28 19:23 libboost_test_exec_monitor.a
-rw-rw-r-- 1 xy xy 2.2M May 28 19:23 libboost_unit_test_framework.a
.......
xy@xy:~/boost_install/lib$ ll -th include/
total 20K
drwxrwxr-x 110 xy xy 12K May 28 19:22 boost/
如果您对如何使用本地安装的Boost感兴趣,则可以看到以下问题:如何让CMake查找替代的Boost安装?。