使用CMake时“无法找到boost库”错误

时间:2013-12-13 20:24:32

标签: c++ boost cmake

我有以下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

我做错了什么?

2 个答案:

答案 0 :(得分: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而不是,这样就更有趣了。

除此之外,您的设置完全正常。

答案 1 :(得分:0)

而不是

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").