CMake找不到Boost libarary

CMake找不到Boost libarary,boost,cmake,Boost,Cmake,我是新来的。我读过同样问题的帖子,但我仍然无法解决我的问题。对于同样的问题,我很抱歉 我的boost include位于:/usr/local/ceral/boost/1.58.0/include中 我的boost库位于:/usr/local/ceral/boost/1.58.0/lib中 我的CMakeLists.txt是: cmake_minimum_required(VERSION 2.6) SET(BOOST_ROOT /usr/local/Cellar/boost/1.58.0) SE

我是新来的。我读过同样问题的帖子,但我仍然无法解决我的问题。对于同样的问题,我很抱歉

我的boost include位于:/usr/local/ceral/boost/1.58.0/include中 我的boost库位于:/usr/local/ceral/boost/1.58.0/lib中

我的CMakeLists.txt是:

cmake_minimum_required(VERSION 2.6)
SET(BOOST_ROOT /usr/local/Cellar/boost/1.58.0)
SET(BOOST_INCLUDEDIR /usr/local/Cellar/boost/1.58.0/include)
SET(BOOST_LIBRARYDIR /usr/local/Cellar/boost/1.58.0/lib)
set(Boost_USE_STATIC_LIBS   ON)
find_package(Boost COMPONENTS optional REQUIRED)

include_directories(${Boost_INCLUDEDIR})
target_link_libraries(hash ${Boost_LIBRARYDIR})
我得到的调试消息是:

-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:551 ]     _boost_TEST_VERSIONS =   1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:553 ] Boost_USE_MULTITHREADED = TRUE
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:555 ] Boost_USE_STATIC_LIBS = ON
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:557 ] Boost_USE_STATIC_RUNTIME = 
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:559 ] Boost_ADDITIONAL_VERSIONS = 
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:561 ] Boost_NO_SYSTEM_PATHS = 
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:613 ] Declared as CMake or Environmental Variables:
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:615 ]   BOOST_ROOT = /usr/local/Cellar/boost/1.58.0
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:617 ]   BOOST_INCLUDEDIR = /usr/local/Cellar/boost/1.58.0/include
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:619 ]   BOOST_LIBRARYDIR = /usr/local/Cellar/boost/1.58.0/lib
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:621 ] _boost_TEST_VERSIONS = 1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:690 ] Include debugging info:
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:692 ]   _boost_INCLUDE_SEARCH_DIRS = /usr/local/Cellar/boost/1.58.0/include;/usr/local/Cellar/boost/1.58.0/include;/usr/local/Cellar/boost/1.58.0;PATHS;C:/boost/include;C:/boost;/sw/local/include
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:694 ]   _boost_PATH_SUFFIXES = boost-1_59_0;boost_1_59_0;boost/boost-1_59_0;boost/boost_1_59_0;boost-1_59;boost_1_59;boost/boost-1_59;boost/boost_1_59;boost-1_58_0;boost_1_58_0;boost/boost-1_58_0;boost/boost_1_58_0;boost-1_58;boost_1_58;boost/boost-1_58;boost/boost_1_58;boost-1_57_0;boost_1_57_0;boost/boost-1_57_0;boost/boost_1_57_0;boost-1_57;boost_1_57;boost/boost-1_57;boost/boost_1_57;boost-1_56_0;boost_1_56_0;boost/boost-1_56_0;boost/boost_1_56_0;boost-1_56;boost_1_56;boost/boost-1_56;boost/boost_1_56;boost-1_55_0;boost_1_55_0;boost/boost-1_55_0;boost/boost_1_55_0;boost-1_55;boost_1_55;boost/boost-1_55;boost/boost_1_55;boost-1_54_0;boost_1_54_0;boost/boost-1_54_0;boost/boost_1_54_0;boost-1_54;boost_1_54;boost/boost-1_54;boost/boost_1_54;boost-1_53_0;boost_1_53_0;boost/boost-1_53_0;boost/boost_1_53_0;boost-1_53;boost_1_53;boost/boost-1_53;boost/boost_1_53;boost-1_52_0;boost_1_52_0;boost/boost-1_52_0;boost/boost_1_52_0;boost-1_52;boost_1_52;boost/boost-1_52;boost/boost_1_52;boost-1_51_0;boost_1_51_0;boost/boost-1_51_0;boost/boost_1_51_0;boost-1_51;boost_1_51;boost/boost-1_51;boost/boost_1_51;boost-1_50_0;boost_1_50_0;boost/boost-1_50_0;boost/boost_1_50_0;boost-1_50;boost_1_50;boost/boost-1_50;boost/boost_1_50;boost-1_49_0;boost_1_49_0;boost/boost-1_49_0;boost/boost_1_49_0;boost-1_49;boost_1_49;boost/boost-1_49;boost/boost_1_49;boost-1_48_0;boost_1_48_0;boost/boost-1_48_0;boost/boost_1_48_0;boost-1_48;boost_1_48;boost/boost-1_48;boost/boost_1_48;boost-1_47_0;boost_1_47_0;boost/boost-1_47_0;boost/boost_1_47_0;boost-1_47;boost_1_47;boost/boost-1_47;boost/boost_1_47;boost-1_46_1;boost_1_46_1;boost/boost-1_46_1;boost/boost_1_46_1;boost-1_46_0;boost_1_46_0;boost/boost-1_46_0;boost/boost_1_46_0;boost-1_46;boost_1_46;boost/boost-1_46;boost/boost_1_46;boost-1_45_0;boost_1_45_0;boost/boost-1_45_0;boost/boost_1_45_0;boost-1_45;boost_1_45;boost/boost-1_45;boost/boost_1_45;boost-1_44_0;boost_1_44_0;boost/boost-1_44_0;boost/boost_1_44_0;boost-1_44;boost_1_44;boost/boost-1_44;boost/boost_1_44;boost-1_43_0;boost_1_43_0;boost/boost-1_43_0;boost/boost_1_43_0;boost-1_43;boost_1_43;boost/boost-1_43;boost/boost_1_43;boost-1_42_0;boost_1_42_0;boost/boost-1_42_0;boost/boost_1_42_0;boost-1_42;boost_1_42;boost/boost-1_42;boost/boost_1_42;boost-1_41_0;boost_1_41_0;boost/boost-1_41_0;boost/boost_1_41_0;boost-1_41;boost_1_41;boost/boost-1_41;boost/boost_1_41;boost-1_40_0;boost_1_40_0;boost/boost-1_40_0;boost/boost_1_40_0;boost-1_40;boost_1_40;boost/boost-1_40;boost/boost_1_40;boost-1_39_0;boost_1_39_0;boost/boost-1_39_0;boost/boost_1_39_0;boost-1_39;boost_1_39;boost/boost-1_39;boost/boost_1_39;boost-1_38_0;boost_1_38_0;boost/boost-1_38_0;boost/boost_1_38_0;boost-1_38;boost_1_38;boost/boost-1_38;boost/boost_1_38;boost-1_37_0;boost_1_37_0;boost/boost-1_37_0;boost/boost_1_37_0;boost-1_37;boost_1_37;boost/boost-1_37;boost/boost_1_37;boost-1_36_1;boost_1_36_1;boost/boost-1_36_1;boost/boost_1_36_1;boost-1_36_0;boost_1_36_0;boost/boost-1_36_0;boost/boost_1_36_0;boost-1_36;boost_1_36;boost/boost-1_36;boost/boost_1_36;boost-1_35_1;boost_1_35_1;boost/boost-1_35_1;boost/boost_1_35_1;boost-1_35_0;boost_1_35_0;boost/boost-1_35_0;boost/boost_1_35_0;boost-1_35;boost_1_35;boost/boost-1_35;boost/boost_1_35;boost-1_34_1;boost_1_34_1;boost/boost-1_34_1;boost/boost_1_34_1;boost-1_34_0;boost_1_34_0;boost/boost-1_34_0;boost/boost_1_34_0;boost-1_34;boost_1_34;boost/boost-1_34;boost/boost_1_34;boost-1_33_1;boost_1_33_1;boost/boost-1_33_1;boost/boost_1_33_1;boost-1_33_0;boost_1_33_0;boost/boost-1_33_0;boost/boost_1_33_0;boost-1_33;boost_1_33;boost/boost-1_33;boost/boost_1_33
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:714 ] location of version.hpp: /usr/local/Cellar/boost/1.58.0/include/boost/version.hpp
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:738 ] version.hpp reveals boost 1.58.0
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:824 ] guessed _boost_COMPILER = 
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:834 ] _boost_MULTITHREADED = -mt
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:877 ] _boost_RELEASE_ABI_TAG = -
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:879 ] _boost_DEBUG_ABI_TAG = -d
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:933 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = /usr/local/Cellar/boost/1.58.0/lib;/usr/local/Cellar/boost/1.58.0/lib;/usr/local/Cellar/boost/1.58.0/stage/lib;/usr/local/Cellar/boost/1.58.0/include/lib;/usr/local/Cellar/boost/1.58.0/include/../lib;/usr/local/Cellar/boost/1.58.0/include/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib_boost_LIBRARY_SEARCH_DIRS_DEBUG   = /usr/local/Cellar/boost/1.58.0/lib;/usr/local/Cellar/boost/1.58.0/lib;/usr/local/Cellar/boost/1.58.0/stage/lib;/usr/local/Cellar/boost/1.58.0/include/lib;/usr/local/Cellar/boost/1.58.0/include/../lib;/usr/local/Cellar/boost/1.58.0/include/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:1045 ] Searching for OPTIONAL_LIBRARY_RELEASE: boost_optional-mt-1_58;boost_optional-mt;boost_optional-mt-1_58;boost_optional-mt;boost_optional
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:1087 ] Searching for OPTIONAL_LIBRARY_DEBUG: boost_optional-mt-d-1_58;boost_optional-mt-d;boost_optional-mt-d-1_58;boost_optional-mt-d;boost_optional-mt;boost_optional
-- [ /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:1153 ] Boost_FOUND = 1
CMake Error at   /usr/local/Cellar/cmake/3.4.1/share/cmake/Modules/FindBoost.cmake:1247 (message):
Unable to find the requested Boost libraries.

Boost version: 1.58.0

Boost include path: /usr/local/Cellar/boost/1.58.0/include

Could not find the following static Boost libraries:

      boost_optional

No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of
Boost.
Call Stack (most recent call first):
CMakeLists.txt:6 (find_package)


CMake Warning (dev) at CMakeLists.txt:9 (target_link_libraries):
Cannot specify link libraries for target "hash" which is not built by this
project.

CMake does not support this but it used to work accidentally and is being
allowed for compatibility.

Policy CMP0016 is not set: target_link_libraries() reports error if its
only argument is not a target.  Run "cmake --help-policy CMP0016" for
policy details.  Use the cmake_policy command to set the policy and
suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring incomplete, errors occurred!
我通过运行cmake来执行

编辑。问题已经解决了。正确的CMakeLists.txt应为:

cmake_minimum_required(VERSION 2.6)
SET(BOOST_ROOT /usr/local/Cellar/boost/1.58.0)
SET(BOOST_INCLUDEDIR /usr/local/Cellar/boost/1.58.0/include)
find_package(Boost REQUIRED)

include_directories(${BOOST_INCLUDEDIR})
add_executable(myProgram main.cc)

发生错误的原因是Boost.Optional是一个只包含头文件的文件,我不需要找到它的库

Boost.Optional是一个仅限头部的库:

find_包中的
组件
仅适用于内置库。所以,你只需要写:

find_package(Boost 1.58.0 REQUIRED)

请参阅类似链接:

好的,Boost。可选的是一个只包含标题的库,因此无法找到它的共享库,根据文档,该库的名称应该在
组件
参数中指定。我想这是问题的根源,您只需搜索boost本身,而不需要任何组件:

find_package(Boost REQUIRED)

在这种情况下,没有额外的预编译库,因此Boost不需要
target\u link\u库
。不需要可选(但项目中的其他第三方LIB可能需要可选)。

因此我将“find\u package(Boost COMPONENTS Optional required)”更改为“find\u package(Boost)”?我需要修改目标链接库吗?它现在不会给我错误消息,但我在使用“cmake”后找不到可执行文件。我按照您的建议更改了查找包并删除了目标链接库行。我还在行的末尾添加了“添加可执行文件(myProgram main.cc)”。我修改了查找包并用“target_link_libraries”。我还在文件末尾添加了“add_executable(myProgram main.cc)”。它没有显示任何错误,但我仍然找不到我的可执行文件。在执行
cmake..
后,运行
make
后,make仍然会告诉我“boost/optional.hpp”文件未找到。(你能打印变量
${boost\u INCLUDEDIR}
?使用:
消息(状态“include dir:${Boost\u INCLUDEDIR}”)
它是空的。它只打印“include dir:”。但是当我打开调试(即-DBoost\u debug=on)时,它会显示“Boost\u INCLUDEDIR=/usr/local/cillar/Boost/1.58.0/include”。为什么会这样?