【问题标题】:"Could not find boost libraries" error when using CMake使用 CMake 时出现“找不到 boost 库”错误
【发布时间】:2013-12-13 20:24:32
【问题描述】:

我有以下 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 2.6)

project (some_project)

set(BOOST_ROOT "E:/libs/boost_1_54_0")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

set (SOURCES 
    main.cpp) 

add_executable (${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

enable_testing ()
add_test (${PROJECT_NAME} ${PROJECT_NAME})

当我尝试将此文件与 CMake 一起使用时,出现以下错误:

cmake.exe .
-- Building for: Visual Studio 12
-- The C compiler identification is MSVC 18.0.21005.1
-- The CXX compiler identification is MSVC 18.0.21005.1
-- Check for working C compiler using: Visual Studio 12
-- Check for working C compiler using: Visual Studio 12 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 12
-- Check for working CXX compiler using: Visual Studio 12 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at e:/software/CMake 2.8/share/cmake-2.8/Modules/FindBoost.cmake:1111 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.54.0

  Boost include path: E:/libs/boost_1_54_0

  Could not find the following static Boost libraries:

          boost_unit_test_framework

  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:8 (find_package)


-- Configuring incomplete, errors occurred!

在“e:\libs\boost_1_54_0\stage\lib\”目录中,我有以下 *.lib 文件:

libboost_unit_test_framework-vc110-mt-1_54.lib
libboost_unit_test_framework-vc110-mt-gd-1_54.lib
libboost_unit_test_framework-vc110-mt-s-1_54.lib
libboost_unit_test_framework-vc110-mt-sgd-1_54.lib

我做错了什么?

【问题讨论】:

  • 仔细阅读 my other answer,尤其是它下面的所有 cmets,关于您的库适用于 VS 11,但您使用的是 VS 12 的事实。
  • @Haroogan 什么? msvc-11.0 是 Visual Studio 2012 的编译器
  • 哦,我的错。我以为它在库中显示 VS 的版本,正确的是 vc110。好的,那么第一个奇怪的是Boost include path: E:/libs/boost_1_54_0。它应该是E:/libs/boost_1_54_0/include。此外,link_directories(${Boost_LIBRARY_DIR}) 是多余的,您最好使用Boost_INCLUDE_DIRS - 复数。
  • 也运行set(Boost_DEBUG ON),看看你得到了什么。
  • 没有。如果您选择 Visual Studio 12 作为您的生成器,它适用于 Visual Studio 2013 而不是 Visual Studio 2012。2 位数年份和版本号不匹配。 Visual Studio 11 是 Visual Studio 2012 的正确生成器。

标签: c++ boost cmake


【解决方案1】:

Visual Studio 的版本号一团糟。 VS10 是 Visual Studio 10,但 VS11 是 Visual Studio 2012,而 VS12 是 Visual Studio 2013。

您只是在 CMake 中选择了错误的生成器。如果您想为 2012 年构建,正确的生成器是 Visual Studio 11。只需删除您的 CMakeCache.txt 并再次运行 CMake with the correct generator

cmake -G "Visual Studio 11" .

哦,当我们这样做的时候:考虑改为out-of-source build,那样会更有趣。

除此之外,您的设置完全没问题。

【讨论】:

    【解决方案2】:

    代替

    set(BOOST_ROOT "E:/libs/boost_1_54_0")
    ...
    find_package(Boost COMPONENTS unit_test_framework REQUIRED)
    

    ,我建议你只尝试

    find_package(Boost COMPONENTS unit_test_framework REQUIRED HINT "E:/libs/boost_1_54_0").
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      相关资源
      最近更新 更多