Question

Newish C++ programmer here. I am creating a QT4 application and it's gotten large enough to where I want to start using log4cplus. I think I'm close but qmake is still not cooperating.

I am running on a Windows machine, and I compiled log4cplus as a static library under cygwin ($ ./configure --enable-static).

First Question
When I compiled log4cplus I got two files.

  • liblog4cplus.a
  • liblog4cplus.dll.a

Do I need to include both of them? What's with the .dll.a file?

Second Question
When I compile moc succeeds but gcc fails when trying to use any of log4cplus classes. I'm not sure if it can't find the header files or if it can't find the actual library.

g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\Qt\2010.05\qt\include\QtCore" -I"..\..\..\Qt\2010.05\qt\include\QtGui" -I"..\..\..\Qt\2010.05\qt\include" -I"external" -I"..\..\..\Qt\2010.05\qt\include\ActiveQt" -I"debug" -I"..\..\..\Qt\2010.05\qt\mkspecs\win32-g++" -o debug\qrc_tilex.o debug\qrc_tilex.cpp
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\tilex.exe object_script.tilex.Debug  -L"c:\work\workspace\tilex\lib" -L"c:\Qt\2010.05\qt\lib" -lmingw32 -lqtmaind -Lliblog4cplus.a -lQtGuid4 -lQtCored4 
./debug\main.o: In function `Z5qMainiPPc':
C:\work\workspace\tilex/main.cpp:37: undefined reference to `log4cplus::Logger::getDefaultHierarchy()'
C:\work\workspace\tilex/main.cpp:37: undefined reference to `log4cplus::BasicConfigurator::BasicConfigurator(log4cplus::Hierarchy&)'
C:\work\workspace\tilex/main.cpp:51: undefined reference to `log4cplus::BasicConfigurator::~BasicConfigurator()'
C:\work\workspace\tilex/main.cpp:51: undefined reference to `log4cplus::BasicConfigurator::~BasicConfigurator()'
mingw32-make[1]: Leaving directory `C:/work/workspace/tilex'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\tilex.exe] Error 1
mingw32-make: *** [debug] Error 2

My project resides in C:\work\workspace\tilex.

and my directory structure is this:

tilex
     /lib
         /<*.a files>
     /external
              /log4cplus
                        /<header files>

Relevant portion of my .pro file. (I've tried several permutations of all these variables, and still get the same result)

INCLUDEPATH += C:\\work\\workspace\\tilex\\external
QMAKE_LIBDIR += C:\\work\\workspace\\tilex\\lib
LIBS += -Lliblog4cplus.a

My main file (which compiles and runs fine without log4cplus).

#include "Tilex.h"
#include <QtGui>
#include <QApplication>

#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>

using namespace log4cplus;

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(tilex);
    QApplication app(argc, argv);

    // Fails
    BasicConfigurator config;
//    config.configure();
//    Logger::getInstance()
//    Logger logger = Logger::getInstance("main");
//    LOG4CPLUS_WARN(logger, "Hello, World!");
    // !

    Tilex mainWin;
    mainWin.show();
    return app.exec();
}
Was it helpful?

Solution

Assuming everything else is correct, you should change your LIBS to this:

LIBS += -llog4cplus

The -L flag tells g++ to set a path to look for libraries. The -l flag tells it to link a specified library (with the lib- prefix and the filetype removed).

If by you mean large as in size instead of complexity when you are saying your application is getting 'large', you might want to ask yourself why you are using static linking in the first place (look at this thread).

OTHER TIPS

You need to put two things on your link line:

-Ldirectory\where\log4c\library\file\lives

and

-llog4cplus

The -L tells the linker to add that directory to its search path. The -l tells the linker to look for the file with lib prepended and .a or .so appended. So -lfoo tells the linker to look for libfoo.a or libfoo.so in the linker search path.

Alternatively you could directly put the full path to the library on the link line without using any -L or -l:

g++ blah blah directory\where\log4c\library\file\lives\liblog4cplus.a blah blah
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top