c++loggingbuildlinkerlog4cplus

log4cplus unresolved external symbol LNK2001


I am trying to use log4cplus as a logger in my project. I have download and compiled log4cplus in VS2013 both 'Debug_Unicode' and 'Release_Unicode' successfully (project compiles successfully in Release and Debug).

In my 'Project Properties->Configuration Properties' I have added:

  1. 'log4cplus\include' to the 'C/C++->All Options->Additional Include Directories'
  2. 'log4cplus\msvc10\Win32\obj.log4cplus.Release_Unicode' to the 'Linker->All Options->Additional Library Directories'
  3. 'log4cplusu.lib' in the 'Linker->All Options->Additional Dependencies'

I have also made sure that I am using the same Runtime Library, in my case Multi-threaded DLL (/MD), in both the log4cplus project as well as in my own project.

But when I try to build my project I am getting the following link errors:

error LNK2001: unresolved external symbol "class
std::basic_ostringstream<char,struct std::char_traits<char>,class
std::allocator<char> > & __cdecl
log4cplus::detail::get_macro_body_oss(void)"
(?get_macro_body_oss@detail@log4cplus@@YAAAV?$basic_ostringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)

error LNK2001: unresolved external symbol "void __cdecl
log4cplus::detail::macro_forced_log(class log4cplus::Logger const
&,int,class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > const &,char const *,int,char const *)"
(?macro_forced_log@detail@log4cplus@@YAXABVLogger@2@HABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBDH2@Z)

fatal error LNK1120: 2 unresolved externals

Solution

  • A Windows UNICODE build is built with TCHAR etc. being defined as wchar_t etc. When not building with UNICODE defined as build with TCHAR defined as char etc.

    Building one library with UNICODE defined and attempting to link it in a project where UNICODE is not defined will result in linker errors since there will be a mismatch in the definition of TCHAR; char vs. wchar_t.

    To correct this, build all the required libraries and projects with a consistent definition of UNICODE (and _UNICODE).

    This can be done with either;

    #define UNICODE
    #define _UNICODE
    

    Or in the project settings;

    Project Properties > General > Project Defaults > Character Set

    Or on the command line;

    /DUNICODE /D_UNICODE
    

    The alternative is applicable as well, if UNICODE is not intended to be used, make sure the defines are not set, and/or the multi-character setting is used in the projects and consistently applied.

    Do not forget to be consistent between the "Release" and "Debug" builds as well.