c++ - 使用CMake时出现"Could not find boost libraries"错误

标签 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

我做错了什么?

最佳答案

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相反,这样会更有趣。

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

关于c++ - 使用CMake时出现"Could not find boost libraries"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575361/

相关文章:

c++ - 从boost 1.73.0包括read_graphviz_new.cpp无法编译

python - 如何配置 Yocto/Boost 以支持 Python 2?

C++ Boost SML 库

ubuntu - 尝试在 Ubuntu 上构建 OpenCV 3 世界模块时出现 CMake 不存在目标错误

c++ - Mac os x 应用程序包在下载和运行时崩溃,但在终端或更改 Info.plist 时运行良好

c++ - Qt toDouble() 方法转换为 int

c++ - 我想确认我对变量作用域的理解在 C++ 中是正确的

c++ - 掩蔽的工作原理

c++ - 如何在 C++ 中对派生类使用 Boost 序列化?

c++ - 在 CMake 中设置默认编译器