Cmake 引用boost库时报错 Could NOT find Boost (missing: filesystem)

在cmake-cookbook中,在Detecting the Boost libraries章节中,需要链接boost的filesystem library
源码:https://github.com/dev-cafe/cmake-cookbook/tree/master/chapter-03/recipe-08
 
通过find_package FindBoost module 寻找boost库, filesystem报错
代码 find_package(Boost 1.54 REQUIRED COMPONENTS filesystem)
 
首先我们下载boost,Boost Downloads,windows平台下载了boost_1_78_0.zip
解压后得到下面的文件
 
下一步编译,由于我没有msvc,用的mingw64 gcc编译,具体编译教程请参考Boost编译与使用 - 知乎 (zhihu.com)
我就默认编译的就得到下面,其中boost文件夹里面时头文件, .\stage\lib下是我们编译好的library

 

 在cmake中运行报错, missing: Boost_INCLUDE_DIR, 这个是boost 头文件所在路径。

具体可参考https://cmake.org/cmake/help/latest/module/FindBoost.html?highlight=findboost

CMake Error at D:/CMake/share/cmake-3.23/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Boost (missing: Boost_INCLUDE_DIR filesystem) (Required is
at least version "1.54")
Call Stack (most recent call first):
D:/CMake/share/cmake-3.23/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
D:/CMake/share/cmake-3.23/Modules/FindBoost.cmake:2375 (find_package_handle_standard_args)
CMakeLists.txt:19 (find_package)

于是我们设置BOOST_ROOT  BOOST_INCLUDEDIR   BOOST_LIBRARYDIR
set(BOOST_ROOT "D:/Boost/boost_1_78_0")
set(BOOST_INCLUDEDIR "D:/Boost/boost_1_78_0/boost")
set(BOOST_LIBRARYDIR "D:/Boost/boost_1_78_0/stage/lib")
cmake -D BOOST_ROOT= "D:/Boost/boost_1_78_0"
cmake -D BOOST_INCLUDEDIR="D:/Boost/boost_1_78_0/boost" -D BOOST_LIBRARYDIR="D:/Boost/boost_1_78_0/stage/lib"

继续执行发现报错

Could NOT find Boost (missing: filesystem) (found suitable version
"1.78.0", minimum required is "1.54")

为了更加清楚发现问题,打开boost debug,再次执行源程序

set(Boost_DEBUG ON)
set(BOOST_ROOT "D:/Boost/boost_1_78_0")
set(BOOST_INCLUDEDIR "D:/Boost/boost_1_78_0/boost")
set(BOOST_LIBRARYDIR "D:/Boost/boost_1_78_0/stage/lib")
find_package(Boost 1.54 REQUIRED COMPONENTS filesystem)
在输出中我们
 Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-mgw11-mt-1_78;boost_filesystem-mgw11-mt;boost_filesystem-mgw11-mt;boost_filesystem-mt-1_78;boost_filesystem-mt;boost_filesystem-mt;boost_filesystem-mt;boost_filesystem
Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-mgw11-mt-d-1_78;boost_filesystem-mgw11-mt-d;boost_filesystem-mgw11-mt-d;boost_filesystem-mt-d-1_78;boost_filesystem-mt-d;boost_filesystem-mt-d;boost_filesystem-mt;boost_filesystem
发现cmake在搜寻filesystem library得时候名字与我们的library不一样,lib前缀  x64 x32后缀

 

 

于是我们添加Boost_LIB_PREFIX 和 Boost_ARCHITECTURE,具体含义详见(两次红色的变化,多了lib前缀,x64后缀

https://cmake.org/cmake/help/latest/module/FindBoost.html?highlight=findboost

set(Boost_DEBUG ON)
set(BOOST_ROOT "D:/Boost/boost_1_78_0")
set(BOOST_INCLUDEDIR "D:/Boost/boost_1_78_0/boost")
set(BOOST_LIBRARYDIR "D:/Boost/boost_1_78_0/stage/lib")
set(Boost_LIB_PREFIX "lib")
set(Boost_ARCHITECTURE "-x64")

find_package(Boost 1.54 REQUIRED COMPONENTS filesystem)
最后成功

Searching for FILESYSTEM_LIBRARY_RELEASE: libboost_filesystem-mgw11-mt-x64-1_78;libboost_filesystem-mgw11-mt-x64;libboost_filesystem-mgw11-mt;libboost_filesystem-mt-x64-1_78;libboost_filesystem-mt-x64;libboost_filesystem-mt;libboost_filesystem-mt;libboost_filesystem

Searching for FILESYSTEM_LIBRARY_DEBUG: libboost_filesystem-mgw11-mt-d-x64-1_78;libboost_filesystem-mgw11-mt-d-x64;libboost_filesystem-mgw11-mt-d;libboost_filesystem-mt-d-x64-1_78;libboost_filesystem-mt-d-x64;libboost_filesystem-mt-d;libboost_filesystem-mt;libboost_filesystem

-- Found Boost: D:/Boost/boost_1_78_0 (found suitable version "1.78.0", minimum required is "1.54") found components: filesystem
-- boost include dirs: D:/Boost/boost_1_78_0
-- boost library dirs: D:/Boost/boost_1_78_0/stage/lib
-- Configuring done
-- Generating done
-- Build files have been written to: E:/computer_project/cmake-cookbook/chapter-03/recipe-08/cxx-example/build

 

 
 才学习CMake,遇到的问题也很多,希望这个能给大家带来一点帮助或思路。
 
posted @ 2022-03-28 16:22  tututan  阅读(17189)  评论(1编辑  收藏  举报