Qt Creator 没有通过 CMake 找到 Boost,CMake 自己找到了

Qt Creator does not find Boost via CMake, CMake itself does

我有一个使用 Boost 和 Qt 并使用 CMake 编译的项目。当我使用 cmake … -DCMAKE_RELEASE_TYPE=Release -G ninja 构建它并使用 ninja 构建它时,它工作得很好。

为了UI调试,我想用Qt Creator。当我用它打开 CMake 项目时,它找不到 Boost:

-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:529 ] _boost_TEST_VERSIONS = 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/share/cmake-3.0/Modules/FindBoost.cmake:531 ] Boost_USE_MULTITHREADED = TRUE
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:533 ] Boost_USE_STATIC_LIBS = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:535 ] Boost_USE_STATIC_RUNTIME = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:537 ] Boost_ADDITIONAL_VERSIONS = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:539 ] Boost_NO_SYSTEM_PATHS = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:591 ] Declared as CMake or Environmental Variables:
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:593 ]   BOOST_ROOT = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:595 ]   BOOST_INCLUDEDIR = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:597 ]   BOOST_LIBRARYDIR = 
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:599 ] _boost_TEST_VERSIONS = 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/share/cmake-3.0/Modules/FindBoost.cmake:692 ] location of version.hpp: /usr/include/boost/version.hpp
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:716 ] version.hpp reveals boost 1.55.0
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:801 ] guessed _boost_COMPILER = -gcc49
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:811 ] _boost_MULTITHREADED = -mt
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:854 ] _boost_RELEASE_ABI_TAG = -
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:856 ] _boost_DEBUG_ABI_TAG = -d
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:905 ] _boost_LIBRARY_SEARCH_DIRS = /usr/include/lib;/usr/include/../lib;/usr/include/../lib/;/usr/include/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1015 ] Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-gcc49-mt-1_55;boost_filesystem-gcc49-mt;boost_filesystem-mt-1_55;boost_filesystem-mt;boost_filesystem
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1051 ] Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-gcc49-mt-d-1_55;boost_filesystem-gcc49-mt-d;boost_filesystem-mt-d-1_55;boost_filesystem-mt-d;boost_filesystem-mt;boost_filesystem
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1015 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-gcc49-mt-1_55;boost_system-gcc49-mt;boost_system-mt-1_55;boost_system-mt;boost_system
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1051 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-gcc49-mt-d-1_55;boost_system-gcc49-mt-d;boost_system-mt-d-1_55;boost_system-mt-d;boost_system-mt;boost_system
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1015 ] Searching for SERIALIZATION_LIBRARY_RELEASE: boost_serialization-gcc49-mt-1_55;boost_serialization-gcc49-mt;boost_serialization-mt-1_55;boost_serialization-mt;boost_serialization
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1051 ] Searching for SERIALIZATION_LIBRARY_DEBUG: boost_serialization-gcc49-mt-d-1_55;boost_serialization-gcc49-mt-d;boost_serialization-mt-d-1_55;boost_serialization-mt-d;boost_serialization-mt;boost_serialization
-- [ /usr/share/cmake-3.0/Modules/FindBoost.cmake:1102 ] Boost_FOUND = 1
CMake Error at /usr/share/cmake-3.0/Modules/FindBoost.cmake:1199 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.55.0

  Boost include path: /usr/include

  Could not find the following Boost libraries:

          boost_filesystem
          boost_system
          boost_serialization

  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:17 (find_package)
-- Configuring incomplete, errors occurred!
See also "…/CMakeOutput.log".
See also "…/CMakeError.log".

顶级 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION 2.8.11)

set(CMAKE_CXX_COMPILER "/usr/bin/g++")
add_definitions("-march=native")
add_definitions("-std=c++11")
add_definitions("-Werror")
add_definitions("-Wall")
add_definitions("-fdiagnostics-color=always")

project(… CXX)

set(Boost_DEBUG ON)

find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS filesystem system serialization)
find_package(OpenMP)

if(OPENMP_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

set(CMAKE_INCLUDE_CURRENT_DIR ON)

file(GLOB cpp_files *.cpp)

add_subdirectory(…)
add_subdirectory(gui)

gui 子目录中我还有一个:

cmake_minimum_required(VERSION 2.8.11)

project(…)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

find_package(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})

file(GLOB cpp_files *.cpp)
file(GLOB ui_files *.ui)

QT4_WRAP_UI(UIS_HDRS ${ui_files})

include_directories(${…_SOURCE_DIR})

add_executable(… ${UIS_HDRS} ${cpp_files})
target_link_libraries(… ${OpenCV_LIBS} Qt4::QtGui backend ${Boost_LIBRARIES})
install(TARGETS … DESTINATION bin)

这一切都在 Kubuntu 15.04 上运行。我不明白为什么CMake从命令行调用它时可以找到Boost,但是从Qt Creator调用它时找不到Boost。我怎样才能让它在 Qt Creator 中也能正常工作?

CMake 似乎没有在 /usr/lib/x86_64-linux-gnu 中寻找库。调试输出显示它正在查找 /usr/include/lib;/usr/include/../lib;/usr/include/../lib/;/usr/include/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib.

如果您使用的是旧版本的 CMake,这可能会通过升级来解决(我相信最近在 FindBoost 模块上做了一些工作)。或者,您应该能够通过适当设置 BOOST_LIBRARYDIR 来指示 CMake 在正确的文件夹中查找 - 即

cmake . -DBOOST_LIBRARYDIR=/usr/lib/x86_64-linux-gnu