1

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
Niall
  • 30,036
  • 10
  • 99
  • 142
Merav Kochavi
  • 4,223
  • 2
  • 32
  • 37
  • I just faced a similar issue with Qt where following link had a suitable solution for me: http://stackoverflow.com/questions/14170770/unresolved-external-symbol-public-virtual-struct-qmetaobject-const-thiscal , Of course, the Q_OBJECT hack won't work... – stefaanv Apr 06 '16 at 09:25

1 Answers1

2

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.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • my log4cplus project has the 'Character Set' property to 'Use Unicode Character Set' in my Release_Unicode and Debug_Unicode configurations which are the configurations used to build my project. The link errors still appear. Is there something I might be missing? – Merav Kochavi Apr 07 '16 at 08:57
  • 1
    Are they set in all the projects? Somehow `UNICODE` is not being set in the project giving the linker error (or it is being unset). Given the definitions for `log4cplus::tostringstream& get_macro_body_oss()` and `typedef std::basic_ostringstream tostringstream;`, `tchar` is dependent on `UNICODE`; `#if defined (UNICODE)\n typedef wchar_t tchar;`. It still looks like the logger library was built with `UNICODE` set and your project doesn't have it set. – Niall Apr 07 '16 at 09:33
  • 1
    Your right, my project was set to Multi-Character, this is because I am using Pantheios which is configured to Multi-Character. I have changed the log4cplus project to settings to Multi-Character, compiled it successfully, updated my projects 'Additional Library Directories' and 'Additional Dependencies' in the Linker node, and now everything is compiling as expected. Thanks! – Merav Kochavi Apr 07 '16 at 09:52
  • I've added a note on the alternative (i.e. not using the UNICODE) to the answer. – Niall Apr 07 '16 at 09:55