easyloggingpp icon indicating copy to clipboard operation
easyloggingpp copied to clipboard

Using easylogging++ in static library possibly breaks build of application

Open Flowkap opened this issue 10 years ago • 14 comments

I want to use easylogging++ in a static libary.

The problem is where to call

_INITIALIZE_EASYLOGGINGPP

I don't want the user of my library to care about which logging I use but it should still be possible to use easylogging++ within the application of the user.

Even if i just use easylogging hidden within the libery code due to the export statements the compilation of an application will break if its using easylogging as well and of course calling the _INITIALIZE_EASYLOGGINGPP macro.

Is there a best practise how to use easylogging++ silently without breaking others code? In the example on https://github.com/easylogging/easyloggingpp/tree/master/samples/STL/shared-static-libs its of course working like charm as no other lib or the application itself is using easylogging.

If anyway the user would like to use easyloggin within the application (nnot knowing of course its being used within the library) the build will break as the library is binded statically to the application and thus the _INITIALIZE_EASYLOGGINGPP macro is called twice (or more if there are any more libs using the logging framework silently)

Why don't implement a kind of "call once guard" into _INITIALIZE_EASYLOGGINGPP?

Regards,

Flowkap

Flowkap avatar Apr 24 '14 09:04 Flowkap

Any comment? I really want to know if its possible. Otherwise I'll have to search for an alternative.

Flowkap avatar May 05 '14 07:05 Flowkap

Hi,

Sorry, I do not have windows access atm in order to check this. Waiting for a contributor for any update

abumq avatar May 05 '14 23:05 abumq

The same applies to linux. Its a C++ linker problem not depending on platform.

2014-05-06 1:19 GMT+02:00 Majid [email protected]:

Hi,

Sorry, I do not have windows access atm in order to check this. Waiting for a contributor for any update

— Reply to this email directly or view it on GitHubhttps://github.com/easylogging/easyloggingpp/issues/172#issuecomment-42252338 .

Flowkap avatar May 07 '14 09:05 Flowkap

Can u send me a sample project in simplest form to reproduce the issue?

easylogging avatar May 08 '14 21:05 easylogging

I've just tried,

  • libA which uses easylogging.
  • application which uses easylogging and links to libA.

Everything seemed to compile and link OK.

arm-none-linux-gnueabi-g++ (Sourcery CodeBench Lite 2012.03-57) 4.6.3

jonface avatar Jun 30 '14 07:06 jonface

Can u send me a sample project in simplest form to reproduce the issue? or close this issue?

Thanks

abumq avatar Aug 05 '14 23:08 abumq

I've took the effort to reproduce this possible bug. Sample code is available here (I've used existing sample code).

I've checked these four situations:

  1. two static libraries using easyloggingpp internally
  2. two shared libraries using easyloggingpp internally
  3. static library and client both using easyloggingpp
  4. shared library and client both using easyloggingpp

Compiling and running succeeded only for case 2. In other cases I got compilation errors (cases 1. and 3.) or crash during main execution (case 4.).

tempaowca avatar Jul 20 '15 16:07 tempaowca

I am encoutering the same problem. I am wondering whether there is a solution to this. In our application, we are using a lot of shared libraries. It is impossible to apply the current Easylogging++ in the individual object files that will be compiled into the shared library, since without the call of "INITIALIZE_EASYLOGGINGPP" the linking of shared library will fail.

tjroamer avatar Nov 26 '15 19:11 tjroamer

I've encountered a similar issue.

My working around was this: make sure INITIALIZE_EASYLOGGINGPP appears EXACTLY ONCE in the entirety of your application (as stated in the readme). This doesn't solve the problem though because it still doesn't let me use ELPP silently in a library without disrupting a client library that uses my library.

So if you have a dependency tree like this: StaticLibA -> ClientA. Where StaticLibA uses ELPP. Then you must make sure ClientA has INITIALIZE_EASYLOGGINGPP somewhere in it. But not StaticLibA. StaticLibA can have #include easylogging++.h but it shouldn't have INITIALIZE_EASYLOGGINGPP.

@mkhan3189, what was the motivation behind INITIALIZE_EASYLOGGINGPP? What does it do? Why must it only appear once?

Edit: to clarify, I believe this is only an issue with static libraries that use ELPP

dassie avatar Jun 18 '16 02:06 dassie

hi @dassie the purpose of this is to assign some extern variables that are used across the library - this is so that we assign them in the beginning and re-use them.

what you are looking for is using INITIALIZE_SHARED_LOGGING

see https://github.com/mkhan3189/project-islam/blob/master/extensions/al-quran/al_quran.cc for example

This project is discontinued but it uses easylogging++ extensively. you can see i initialized easylogging++ once in https://github.com/mkhan3189/project-islam/blob/master/main.cc (it uses old macro name starting with underscore but it's only name, the underlying functionality is same) and did not re-assign it anywhere. Each extension is a library (based on Qt API)

abumq avatar Jun 20 '16 00:06 abumq

static lib build failed

Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial

$ easyloggingpp/samples/STL/shared-static-libs# ./compile_static.sh ar: creating myLib.a a - mylib.o a - easylogging++.o myLib.a(mylib.o):(.bss+0x0): multiple definition of el::base::elStorage' /tmp/ccoZwiZc.o:(.bss+0x0): first defined here myLib.a(mylib.o): In function std::__shared_ptr<el::base::Storage, (__gnu_cxx::_Lock_policy)2>::operator->() const': /usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:251: multiple definition of el::elCrashHandler' /tmp/ccoZwiZc.o:/usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:251: first defined here /usr/bin/ld: Warning: size of symbol _ZN2el14elCrashHandlerE' changed from 1 in /tmp/ccoZwiZc.o to 8 in myLib.a(mylib.o) collect2: error: ld returned 1 exit status

ArmstrongYang avatar Jan 29 '19 06:01 ArmstrongYang

I am experiencing the same issue. I can not find a proper macro to use that will let me compile my project. I keep getting linker errors

afwilkin avatar May 04 '20 02:05 afwilkin

Not sure how active this is. I just stumbled over this and the following worked for me:

#ifndef INITIALIZE_EASYLOGGINGPP
   INITIALIZE_EASYLOGGINGPP
#endif

bastianhjaeger avatar Aug 26 '21 12:08 bastianhjaeger

I'm still encounting this issue, I'm in situation#4, a shared library used easylogging, and I tried to use this shared lib in my app, but failed with segment fault during running the app.

seliber avatar Jan 18 '22 07:01 seliber