Error Obtained
Getting linker error LNK2019 when linking boost libraries.
Environment
To Reproduce
Steps to reproduce the behavior:
See the attached cmakelists.txt for the makelist that I used to see this issue.
Expected behavior
Build with no errors
Failure logs
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl boost::this_thread::interruptible_wait(void *,struct boost::detail::mono_platform_timepoint const &)" (__imp_?interruptible_wait@this_thread@boost@@YA_NPEAXAEBUmono_platform_timepoint@detail@2@@Z) referenced in function "void __cdecl boost::this_thread::sleep_for<__int64,class boost::ratio<1,1> >(class boost::chrono::duration<__int64,class boost::ratio<1,1> > const &)" (??$sleep_for@_JV?$ratio@$00$00@boost@@@this_thread@boost@@YAXAEBV?$duration@_JV?$ratio@$00$00@boost@@@chrono@1@@Z)
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: void __cdecl boost::thread::start_thread(void)" (__imp_?start_thread@thread@boost@@AEAAXXZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) private: static class boost::intrusive_ptr<struct boost::detail::thread_data_base> __cdecl boost::thread::make_thread_info(void (__cdecl*)(void))" (__imp_?make_thread_info@thread@boost@@CA?AV?$intrusive_ptr@Uthread_data_base@detail@boost@@@2@P6AXXZ@Z) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl boost::thread::~thread(void)" (__imp_??1thread@boost@@QEAA@XZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl boost::thread::join(void)" (__imp_?join@thread@boost@@QEAAXXZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::thread::id __cdecl boost::this_thread::get_id(void)" (__imp_?get_id@this_thread@boost@@YA?AVid@thread@2@XZ) referenced in function "void __cdecl thread(void)" (?thread@@YAXXZ)
2>main.obj : error LNK2001: unresolved external symbol "public: __cdecl boost::thread::~thread(void)" (??1thread@boost@@QEAA@XZ)
2>E:\repos\boost-test\build2019\Release\Example.exe : fatal error LNK1120: 7 unresolved externals
@jackboosy this is not a port bug, question was better
@aditya369007 your CMakeLists is not the best example of boost usage with cmake. In any case you are missing a reference to boost-thread library to link
Hi @aditya369007, you can use find_package(Boost REQUIRED COMPONENTS <...>) to find boost libraries and include path using vcpkg tool-chain(command .\vcpkg integrate install).
The following cmake code is correct:
# CMakeList.txt : CMake project for CMakeProject10, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
add_executable (CMakeProject10 "CMakeProject10.cpp" "CMakeProject10.h")
# TODO: Add tests and install targets if needed.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
find_package(Boost REQUIRED COMPONENTS thread)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(CMakeProject10 ${Boost_LIBRARIES})
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang|GNU")
target_compile_options(CMakeProject10 PRIVATE -Wall -Wextra -Wunreachable-code -Wpedantic)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(CMakeProject10 PRIVATE -Wweak-vtables -Wexit-time-destructors -Wglobal-constructors -Wmissing-noreturn )
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_options(CMakeProject10 PRIVATE /W4 /w44265 /w44061 /w44062 )
endif()
Thanks.
@LilyWangL the find_package(Boost REQUIRED COMPONENTS thread) did the trick. Works like a charm. Thank you!
Most helpful comment
Hi @aditya369007, you can use
find_package(Boost REQUIRED COMPONENTS <...>)to find boost libraries and include path using vcpkg tool-chain(command.\vcpkg integrate install).The following cmake code is correct:
Thanks.