尝试将Boost与CMake

时间:2015-07-29 04:02:29

标签: c++ boost visual-studio-2013 cmake

我正在制作我的第一个使用CMake构建系统的项目。我还认为这是尝试将提升融入我的c ++技能的好时机。我已经阅读了文档并浏览了SO和其他网站,但我遇到了LNK2019错误。我读到here这可能是由于32/64位项目/升级库不匹配造成的。但是,我有理由相信我建立了正确的库。我将在

下面发布相关的cmake,代码,错误和命令

构建命令

b2.exe -a variant=debug,release link=shared,static threading=multi address-model=64

生成项目命令

cmake .. -G"Visual Studio 12 Win64"

CMake文件

cmake_minimum_required (VERSION 2.6)
project (Thoth)

add_definitions( -DBOOST_ALL_NO_LIB )
set(Boost_USE_STATIC_LIBS OFF )
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

configure_file (
  "${PROJECT_SOURCE_DIR}/main.h.in"
  "${PROJECT_BINARY_DIR}/main.h"
  )

set(BOOST_ROOT D:/Programs/boost_1_58_0)
set(BOOST_INCLUDEDIR ${BOOST_ROOT})
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib/)
find_package(Boost 1.58.0 COMPONENTS system filesystem REQUIRED)

include_directories("${PROJECT_BINARY_DIR}")

add_executable(Thoth main.cpp)

include_directories(${BOOST_INCLUDEDIR})
link_directories(${BOOST_LIBRARYDIR})

target_link_libraries( Thoth $(Boost_LIBRARIES))

C ++文件

#define BOOST_LIB_DIAGNOSTIC

#include <iostream>
#include <fstream>
#include <string>
#include <boost/filesystem.hpp>

#include "main.h"

using namespace std;

int main(int argc, char *argv[]){
    string line;
    ifstream input("../images/source/balls2.nff", ios::in);
    if (!input){
        cout << "Unable to open file";
        exit(1);
    }
    else{
        while (getline(input, line)){
            cout << line << '\n';
        }

        input.close();
    }

    return 0;
}

Visual Studio输出

------ Build started: Project: ZERO_CHECK, Configuration: Debug x64 ------
------ Build started: Project: Thoth, Configuration: Debug x64 ------
main.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat@system@boost@@YAXXZ)
main.obj : error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category@system@boost@@YAAEBVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'errno_ecat''(void)" (??__Eerrno_ecat@system@boost@@YAXXZ)
C:\Users\JR\Documents\Visual Studio 2013\Projects\thoth\_build64\Debug\Thoth.exe : fatal error LNK1120: 2 unresolved externals
------ Skipped Build: Project: ALL_BUILD, Configuration: Debug x64 ------
Project not selected to build for this solution configuration 
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 1 skipped ==========

希望我忽略了一些愚蠢的事情,但我一直在阅读其他人的答案,而不是一点儿都没有。我觉得是时候寻求帮助了。

反馈后

我会在这里保留最新的CMake和调试输出。这样其他用户(或我)就可以比较原始代码和最终代码。

CMake的

cmake_minimum_required (VERSION 2.6)
project (Thoth)

set(Boost_USE_STATIC_LIBS OFF )
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

configure_file (
  "${PROJECT_SOURCE_DIR}/main.h.in"
  "${PROJECT_BINARY_DIR}/main.h"
  )

set (Boost_DEBUG ON)
set(BOOST_ROOT D:/Programs/boost_1_58_0)
find_package(Boost 1.58.0 COMPONENTS system filesystem REQUIRED)
set(Boost_INCLUDE_DIRS ${BOOST_ROOT})
set(Boost_LIBRARY_DIRS ${BOOST_ROOT}/stage/lib/)
set(Boost_LIBRARIES ${BOOST_ROOT}/stage/lib/)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARIES})

include_directories("${PROJECT_BINARY_DIR}")

add_executable(Thoth main.cpp)

if (WIN32)
    add_definitions( -DBOOST_ALL_NO_LIB )
    add_definitions( -DBOOST_ALL_DYN_LINK )
endif()

target_link_libraries( Thoth $(Boost_LIBRARIES))

启用调试时的CMake命令输出(为了便于阅读,删除了FindBoost的完整路径)

cmake .. -G"Visual Studio 12 Win64"
-- [ FindBoost.cmake:513 ] _boost_TEST_VERSIONS =
-- [ FindBoost.cmake:515 ] Boost_USE_MULTITHREADED = ON
-- [ FindBoost.cmake:517 ] Boost_USE_STATIC_LIBS = OFF
-- [ FindBoost.cmake:519 ] Boost_USE_STATIC_RUNTIME = OFF
-- [ FindBoost.cmake:521 ] Boost_ADDITIONAL_VERSIONS =
-- [ FindBoost.cmake:523 ] Boost_NO_SYSTEM_PATHS =
-- [ FindBoost.cmake:575 ] Declared as CMake or Environmental Variables:
-- [ FindBoost.cmake:577 ]   BOOST_ROOT = D:/Programs/boost_1_58_0
-- [ FindBoost.cmake:579 ]   BOOST_INCLUDEDIR =
-- [ FindBoost.cmake:581 ]   BOOST_LIBRARYDIR =
-- [ FindBoost.cmake:583 ] _boost_TEST_VERSIONS =
-- [ FindBoost.cmake:652 ] Include debugging info:
-- [ FindBoost.cmake:654 ]   _boost_INCLUDE_SEARCH_DIRS =
D:/Programs/boost_1_58_0/include;D:/Programs/boost_1_58_0;PATHS;C:/boost/include;C:/boost;/sw/local/include
-- [ C:/Program Files (x86)/CMake/share/cmake-3.0/Modules/FindBoost.cmake:656 ]   _boost_PATH_SUFFIXES =
-- [ FindBoost.cmake:676 ] location of version.hpp: D:/Programs/boost_1_58_0/boost/version.hpp
-- [ FindBoost.cmake:700 ] version.hpp reveals boost 1.58.0
-- [ FindBoost.cmake:785 ] guessed _boost_COMPILER = -vc120
-- [ FindBoost.cmake:795 ] _boost_MULTITHREADED = -mt
-- [ FindBoost.cmake:838 ] _boost_RELEASE_ABI_TAG = -
-- [ FindBoost.cmake:840 ] _boost_DEBUG_ABI_TAG = -gd
-- [ FindBoost.cmake:888 ] _boost_LIBRARY_SEARCH_DIRS = D:/Programs/boost_1_58_0/lib;D:/Programs/boost_1_58_0/stage/lib;D:/Programs/boost_1_58_0/lib;D:/Programs/boost_1_58_0/../lib;D:/Programs/boost_1_58_0/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
-- [ FindBoost.cmake:998 ] Searching for SYSTEM_LIBRARY_RELEASE: boost_system-vc120-mt-1_58;boost_system-vc120-mt;boost_system-mt-1_58;boost_system-mt;boost_system
-- [ FindBoost.cmake:1034 ] Searching for SYSTEM_LIBRARY_DEBUG: boost_system-vc120-mt-gd-1_58;boost_system-vc120-mt-gd;boost_system-mt-gd-1_58;boost_system-mt-gd;boost_system-mt;boost_system
-- [ FindBoost.cmake:998 ] Searching for FILESYSTEM_LIBRARY_RELEASE: boost_filesystem-vc120-mt-1_58;boost_filesystem-vc120-mt;boost_filesystem-mt-1_58;boost_filesystem-mt;boost_filesystem
-- [ FindBoost.cmake:1034 ] Searching for FILESYSTEM_LIBRARY_DEBUG: boost_filesystem-vc120-mt-gd-1_58;boost_filesystem-vc120-mt-gd;boost_filesystem-mt-gd-1_58;boost_filesystem-mt-gd;boost_filesystem-mt;boost_filesystem
-- [ FindBoost.cmake:1085 ] Boost_FOUND = 1
-- Boost version: 1.58.0
-- Found the following Boost libraries:
  --   system
  --   filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/JR/Documents/Visual Studio 2013/Projects/thoth/_build64

1 个答案:

答案 0 :(得分:3)

这里有两个相当微不足道的问题,希望很容易解决。

首先,find_package(Boost ...)可能正确地完成了工作,即它为Boost_INCLUDE_DIRSBoost_LIBRARY_DIRSBoost_LIBRARIES设置了良好的值。但是,您可以通过调用三个set命令立即覆盖这些变量!所以你应该删除这三个命令。

另一个问题出在target_link_libraries来电中 - 您在尝试取消引用Boost_LIBRARIES时意外使用了括号而不是大括号 - 即将$(Boost_LIBRARIES)更改为${Boost_LIBRARIES}


顺便说一句,还有一些与此问题无关的其他要点:

  1. 你不应该在CMakeLists.txt中设置BOOST_ROOT。它实际上是通过在命令行(或通过CMake GUI)将其作为-D arg传递来设置的,这样它就不会被硬编码为特定于您自己的值个人机器。

  2. 您不需要link_directories来电。 own documentation阻止其使用:

      

    请注意,此命令很少需要。 find_package()find_library()返回的图书馆位置是绝对路径。将这些绝对库文件路径直接传递给target_link_libraries()命令。 CMake将确保链接器找到它们。