5

community,

I have the following problem with building and using the Log4cplus library with Embarcadero. First, I download the library from http://sourceforge.net/p/log4cplus/wiki/Home/, then I navigate to a Directory where I want to build my library, and type cmake -G "Borland Makefiles" mypathtotherootomypreviousdownload. It then says, the build files have been written to the current directory. Now I start the MSYS sh.exe and type make. This does also work. I get a bin Folder with various working tests (as exe files) of the library. In this bin folder there is also a dll log4cplusUD.dll. Now I also find in a src folder the corresponding LIB log4cplusUD.lib. I am of course familiar how you can statically link a dynamic library with Embarcadero. But on attempting to compile

#pragma hdrstop
#pragma argsused

#ifdef _WIN32
#include <tchar.h>
#else
  typedef char _TCHAR;
  #define _tmain main
#endif

#include <stdio.h>

#include "log4cplus/logger.h"
#include "log4cplus/consoleappender.h"
#include "log4cplus/loglevel.h"
#include <log4cplus/loggingmacros.h>
#include <iomanip>

using namespace std;
using namespace log4cplus;



 int _tmain(int argc, _TCHAR* argv[]) 
{

    log4cplus::initialize ();
    SharedAppenderPtr append_1(new ConsoleAppender());
    append_1->setName(LOG4CPLUS_TEXT("First"));
    Logger::getRoot().addAppender(append_1);

    Logger root = Logger::getRoot();
    Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));

    LOG4CPLUS_DEBUG(root,
                    "This is"
                    << " a reall"
                    << "y long message." << endl
                    << "Just testing it out" << endl
                    << "What do you think?");
    test.setLogLevel(NOT_SET_LOG_LEVEL);
    LOG4CPLUS_DEBUG(test, "This is a bool: " << true);
    LOG4CPLUS_INFO(test, "This is a char: " << 'x');
    LOG4CPLUS_INFO(test, "This is a short: " << static_cast<short>(-100));
    LOG4CPLUS_INFO(test, "This is a unsigned short: "
        << static_cast<unsigned short>(100));
    LOG4CPLUS_INFO(test, "This is a int: " << 1000);
    LOG4CPLUS_INFO(test, "This is a unsigned int: " << 1000u);
    LOG4CPLUS_INFO(test, "This is a long(hex): " << hex << 100000000l);
    LOG4CPLUS_INFO(test, "This is a unsigned long: " << 100000000ul);
    LOG4CPLUS_WARN(test, "This is a float: " << 1.2345f);
    LOG4CPLUS_ERROR(test,
                    "This is a double: "
                    << setprecision(15)
                    << 1.2345234234);
    LOG4CPLUS_FATAL(test,
                    "This is a long double: "
                    << setprecision(15)
                    << 123452342342.25L);
    LOG4CPLUS_WARN(test, "The following message is empty:");
    LOG4CPLUS_WARN(test, "");

    return 0;
}

I get a linker error, not resolved external Symbol log4cplus::Logger::getInstance.

The code above is just one of the tests that compiled and run well, when running make from sh.exe. So what am I doing wrong?

I also tried to link the above main.cpp againts log4cplusUD.lib with the following CMakeLists.txt file

cmake_minimum_required (VERSION 2.6)
project (log4cplustest)

include_directories("C:/Users/fin/Software/log4cplus-1.2.0-rc3/log4cplus-1.2.0-rc3/include")
add_executable(log4cplustest main.cpp)
target_link_libraries(log4cplustest log4cplusUD)

but I get the same Linker error!

mathgenius
  • 165
  • 1
  • 13
  • So if I replace target_link_libraries(log4cplustest log4cplusUD) with target_link_libraries(log4cplustest log4cplusUD.dll) I get the same linker error. – mathgenius Apr 15 '15 at 08:55
  • That means that so far the linker has always been trying to link against the dynamic library, which is apparently in your linking path. Try to rename the static library like log4cplusUD_static.lib and link against it. – Antonio Apr 15 '15 at 08:57
  • It didn't work either. I renamed log4cplusUD.lib to log4cplusUD_static.lib and put in my CMakeLists.txt file target_link_libraries(log4cplustest log4cplusUD_static) and get the same linker error. It is strange because I have a lot of working exe's as described in my original post. – mathgenius Apr 15 '15 at 09:03
  • 1
    Ok, now I finally managed to compile. I checked how the tests I mentioned where actually compiled from command line in the CMakeFiles and was thus able to define those Macros -DWin32 -Dlog4cplus_EXPORTS -DUNICODE -D_UNICODE. With all of them, it worked. – mathgenius Apr 15 '15 at 09:59
  • You should post an answer to your own question – Antonio Apr 15 '15 at 10:22

0 Answers0