[英]Hitting a LNK2019 error trying to link Boost with CMake
我正在制作我的第一個將CMake用於構建系統的項目。 我還決定這是一個嘗試將boost納入我的c ++技能的好時機。 我已經閱讀了文檔並瀏覽了SO和其他網站,但我陷入了LNK2019錯誤。 我在這里讀到這可能是由於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}
。
順便說一下,還有其他一些與該問題無關的要點:
通常,您不應該在CMakeLists.txt中設置BOOST_ROOT
。 真正的目的是通過在命令行上(或通過CMake GUI)將其作為-D
arg傳遞來進行設置,從而不會將其硬編碼為特定於您自己的計算機的值。
您不需要link_directories
調用。 它自己的文檔不鼓勵其使用:
請注意,此命令很少需要。
find_package()
和find_library()
返回的庫位置是絕對路徑。 將這些絕對庫文件路徑直接傳遞給target_link_libraries()
命令。 CMake將確保鏈接器找到它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.