我正在制作我的第一个将CMake用于构建系统的项目。我还决定这是一个尝试将boost融入我的c++技能的好时机。我已经阅读了文档并浏览了SO和其他网站,但我陷入了LNK2019错误。我读了here,这可能是由于32/64位项目/ boost库不匹配。但是,我有足够的信心建立正确的库。我将在下面发布相关的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
最佳答案
这里有两个相当琐碎的问题,它们应该很容易解决。
首先是find_package(Boost ...)
可能正在正确地执行其工作,即它正在为Boost_INCLUDE_DIRS
,Boost_LIBRARY_DIRS
和Boost_LIBRARIES
设置良好的值。但是,您可以通过调用三个set
命令立即覆盖这些变量!因此,您应该删除这三个命令。
另一个问题是您的target_link_libraries
调用-您在尝试取消引用Boost_LIBRARIES
时不小心使用了括号而不是大括号-即将$(Boost_LIBRARIES)
更改为${Boost_LIBRARIES}
。
顺便说一下,还有其他一些与该问题无关的要点:
BOOST_ROOT
的代码。真正的目的是通过在命令行上(或通过CMake GUI)将其作为-D
arg传递来进行设置,这样就不会将其硬编码为特定于您自己的计算机的值。 link_directories
调用。它的own documentation不鼓励使用:关于c++ - 尝试将Boost与CMake链接时出现LNK2019错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31691035/