I want to use Boost.Log in a solution with a DLL and an application consuming the DLL. I am using Visual Studio 2022 and vcpkg. I am not using CMake.
// === DLL Project: ===
// Logger.hpp
#pragma once
#define BOOST_LOG_DYN_LINK 1
#include <boost/log/common.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
enum Severity {
kFatal,
kError,
kWarning,
kInfo,
kTrace
};
BOOST_LOG_GLOBAL_LOGGER(gLogger, boost::log::sources::severity_channel_logger_mt<Severity>)
#define LOG_ERROR(...) BOOST_LOG_SEV(gLogger::get(), Severity::kError) << std::format(##__VA_ARGS__)
// Logger.cpp
BOOST_LOG_GLOBAL_LOGGER_INIT(gLogger, boost::log::sources::severity_channel_logger_mt) {
boost::log::sources::severity_channel_logger_mt<Severity> lg;
return lg;
}
// === Application: ===
// Application.cpp
#include "Logger.hpp"
int main(int argc, char** argv) {
try {
prcd::Application app{};
app.Run();
return 0;
} catch (const std::exception& e) {
LOG_ERROR("{}", e.what());
return -1;
}
}
I am using vcpkg, my vcpkg.json
looks like this:
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
"name": "example",
"version-string": "alpha",
"dependencies": [
{
"name": "boost-log",
"version>=": "1.79.0"
}
],
"builtin-baseline": "3d8f78171a2a37d461077bf8d063256b63e25a4f"
}
When I try to compile the project I get many unresolved external symbol errors:
1>Logger.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void * __cdecl boost::log::v2_mt_nt62::attribute::impl::operator new(unsigned __int64)" (__imp_??2impl@attribute@v2_mt_nt62@log@boost@@SAPEAX_K@Z) referenced in function "public: __cdecl boost::log::v2_mt_nt62::attributes::mutable_constant<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,void,void,void>::mutable_constant<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,void,void,void>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)" (??0?$mutable_constant@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XXX@attributes@v2_mt_nt62@log@boost@@QEAA@$$QEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Logger.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl boost::log::v2_mt_nt62::attribute::impl::operator delete(void *,unsigned __int64)" (__imp_??3impl@attribute@v2_mt_nt62@log@boost@@SAXPEAX_K@Z) referenced in function "int `public: __cdecl boost::log::v2_mt_nt62::attributes::mutable_constant<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,void,void,void>::mutable_constant<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,void,void,void>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)'::`1'::dtor$0" (?dtor$0@?0???0?$mutable_constant@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XXX@attributes@v2_mt_nt62@log@boost@@QEAA@$$QEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z@4HA)
1>Logger.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl boost::log::v2_mt_nt62::attribute_set::attribute_set(void)" (__imp_??0attribute_set@v2_mt_nt62@log@boost@@QEAA@XZ) referenced in function "public: __cdecl boost::log::v2_mt_nt62::sources::basic_logger<char,class boost::log::v2_mt_nt62::sources::severity_channel_logger_mt<enum prcd::log::Severity,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,struct boost::log::v2_mt_nt62::sources::multi_thread_model<class boost::log::v2_mt_nt62::aux::light_rw_mutex> >::basic_logger<char,class boost::log::v2_mt_nt62::sources::severity_channel_logger_mt<enum prcd::log::Severity,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,struct boost::log::v2_mt_nt62::sources::multi_thread_model<class boost::log::v2_mt_nt62::aux::light_rw_mutex> >(void)" (??0?$basic_logger@DV?$severity_channel_logger_mt@W4Severity@log@prcd@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@sources@v2_mt_nt62@log@boost@@U?$multi_thread_model@Vlight_rw_mutex@aux@v2_mt_nt62@log@boost@@@2345@@sources@v2_mt_nt62@log@boost@@QEAA@XZ)
1>Logger.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: struct std::pair<class boost::log::v2_mt_nt62::attribute_set::iter<0>,bool> __cdecl boost::log::v2_mt_nt62::attribute_set::insert(class boost::log::v2_mt_nt62::attribute_name,class boost::log::v2_mt_nt62::attribute const &)" (__imp_?insert@attribute_set@v2_mt_nt62@log@boost@@QEAA?AU?$pair@V?$iter@$0A@@attribute_set@v2_mt_nt62@log@boost@@_N@std@@Vattribute_name@234@AEBVattribute@234@@Z) referenced in function "protected: struct std::pair<class boost::log::v2_mt_nt62::attribute_set::iter<0>,bool> __cdecl boost::log::v2_mt_nt62::sources::basic_logger<char,class boost::log::v2_mt_nt62::sources::severity_channel_logger_mt<enum prcd::log::Severity,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,struct boost::log::v2_mt_nt62::sources::multi_thread_model<class boost::log::v2_mt_nt62::aux::light_rw_mutex> >::add_attribute_unlocked(class boost::log::v2_mt_nt62::attribute_name const &,class boost::log::v2_mt_nt62::attribute const &)" (?add_attribute_unlocked@?$basic_logger@DV?$severity_channel_logger_mt@W4Severity@log@prcd@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@sources@v2_mt_nt62@log@boost@@U?$multi_thread_model@Vlight_rw_mutex@aux@v2_mt_nt62@log@boost@@@2345@@sources@v2_mt_nt62@log@boost@@IEAA?AU?$pair@V?$iter@$0A@@attribute_set@v2_mt_nt62@log@boost@@_N@std@@AEBVattribute_name@345@AEBVattribute@345@@Z)
1>Logger.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class boost::shared_ptr<class boost::log::v2_mt_nt62::core> __cdecl boost::log::v2_mt_nt62::core::get(void)" (__imp_?get@core@v2_mt_nt62@log@boost@@SA?AV?$shared_ptr@Vcore@v2_mt_nt62@log@boost@@@4@XZ) referenced in function "public: __cdecl boost::log::v2_mt_nt62::sources::basic_logger<char,class boost::log::v2_mt_nt62::sources::severity_channel_logger_mt<enum prcd::log::Severity,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,struct boost::log::v2_mt_nt62::sources::multi_thread_model<class boost::log::v2_mt_nt62::aux::light_rw_mutex> >::basic_logger<char,class boost::log::v2_mt_nt62::sources::severity_channel_logger_mt<enum prcd::log::Severity,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,struct boost::log::v2_mt_nt62::sources::multi_thread_model<class boost::log::v2_mt_nt62::aux::light_rw_mutex> >(void)" (??0?$basic_logger@DV?$severity_channel_logger_mt@W4Severity@log@prcd@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@sources@v2_mt_nt62@log@boost@@U?$multi_thread_model@Vlight_rw_mutex@aux@v2_mt_nt62@log@boost@@@2345@@sources@v2_mt_nt62@log@boost@@QEAA@XZ)
I suspect I need to tell the linker where to find the libs for Boost.Log but I'm not sure how. For other libraries it "just works" with the setup.
The reason was that vcpkg builds boost for Win 7 and up. You need to #define BOOST_USE_WINAPI_VERSION BOOST_WINAPI_VERSION_WIN7
, and everything works as expected.