Link to home
Start Free TrialLog in
Avatar of dleehanson
dleehansonFlag for United States of America

asked on

Using log4cplus in Visual Studio 2005

Hello,

I am kind of green when it comes to using Visual Studio 2005.  How can I import external libraries, more specifically log4cplus, into Visual Studio?  If I could integrate it fully with source code and documentation intact that would be even better.  Thanks in advance for any help.

--Charly
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

When you say import are you referring to a static library, a dynamic library or a source code library?
Avatar of dleehanson

ASKER

I think I want a Source Code Library?  

I ultimately want to utilize their code library in my own programs, but also have access to the source code and documentation for debugging purposes.  

--Charly
Ok, it seems log4cplus is a source code tarball.

You need to download it and untar it...

type 'tar xvzf  log4cplus-1.0.2.tar.gz'
You will then have a directory called something like 'log4cplus-1.0.2'
cd into that directory and type './configure'
This will run the auto-config tool and generate a makefile
Then type 'make' to build it
Usually after this you type 'make install' to install the library
I would urge you to viewthe README file, which will give you specifics on how to build and link against this library
Would not be much different than Log4Net

http://www.codeproject.com/useritems/Log4cplus.asp
Try this one, hope this helps.

Best Regards,
DeepuAbrahamK
To link to a library you use the -l switch on gcc and to tell gcc where the library lives you use the -L switch.
So if the library is  /usr/local/lib/liblogcplus.so

g++ <other stuff> -L /usr/local/lib/ -l logcplus
Well, log4cplus will be not so easy to compile with VS2005 (see the discussion at  http://www.thescripts.com/forum/thread611267.html). log4cxx however looks more promising, see http://www.dreamcubes.com/blog/?itemid=43 ("log4cxx for Win32 with VS2005") which provides

VS2005 Binary Build of log4cxx v0.10.0 http://www.dreamcubes.com/webdrive/log4cxx_win32/log4cxx-bin-0.10.0-win32-vs2005.rar
VS2005 Project + Source for log4cxx v0.10.0 http://www.dreamcubes.com/webdrive/log4cxx_win32/log4cxx-src-0.10.0-win32-vs2005.rar
Reading...

--Charly
BTW, all you need to do with http://www.dreamcubes.com/webdrive/log4cxx_win32/log4cxx-bin-0.10.0-win32-vs2005.rar is adding the correct library to your project - either by using the IDE to do that by choosing "Project|Add Existing Item", navigating to the lib and double-clicking it or using

#pragma comment(lib,"log4cxx.lib")

and adding

#include "log4xx.h"

to the shource files where you need to use that library.
Ok, I took a look at what everyone sent me, and I think I am going to give log4cxx a shot.  I downloaded the vs2005 project from the link jkr sent me and got it to build successfully.  

Now that I have done that, what is the best way to reference it?  It built 4 different projects: apr, aprutil, log4cxx_dll, and log4cxx_static.  Do I just  "Add Existing Project" and select the log4cxx_static project?  

If yes, how would I go about referencing source files from another project?  Is it simply:

#include "log4cxx_static/someFile.h"

Thanks for all the help everyone.  I really appreciate you taking the time to step me through all this...

--Charly
It is probably simpler to statically link to the library so it has no external dependencies. Just add the library to your project. As for the headers, you can either access them using relative path with the #include "" syntax or if you add the path where the library headers lives into your standard include path you can access the headers direct using the #include <> syntax. You should really choose what works best for you.
SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Argh.  Forgot to refresh before I posted...

I opted to download and build the vs2005 Project +Source fie instead of the binary.  Is that going to be a problem?  I just looked around a bit for "log4cxx.lib" and wasn't able to find it in my project.  Any pointers for tracking it down?

--Charly
Have you built the source? If so try using Windows search from the top folder of the log4cxx project I guess :)
I'd rather go for the DLL, but that's a matter of personal taste.

>>Do I just  "Add Existing Project" and select the log4cxx_static project?  

You can, but you don't have to. Either copy the libs to a separate 'lib' directory of your project and add the path under your project settings (in the "Linker" section) or you leave them where they are and add that path. The same applies to the header files. But unless you are planning to build log4cxx frequently, there is no need to add that project to your solution.
Thanks guys.  I'll check out the link to that walkthrough evilrix sent me and begin the battle anew tomorrow!

--Charly
>>I just looked around a bit for "log4cxx.lib" and wasn't able to find it in my project.  Any pointers for tracking it down?
try log4cxx-src-0.10.0-win32-vs2005\build\<debug|release>\static
The fight is just beginning again, and morale is already low...

So far I followed jkr's advice and created a lib folder and copied the file "log4cxx/build/debug/static/log4cxxd.lib"  into it.  Then I went into Linker  > General > Additional Library Dependencies and added my new lib folder (./lib).

No dice.  

I also tried referencing the whole log4cxx folder, which seems to follow the directory structure of the examples, but even then the compiler couldn't find the required source files.

What am I doing wrong? This shouldn't be that difficult. It doesn't help that I am getting a bit confused as there are so many ways of doing it...

Regrading the static library, it looks like you need to specify every single code file you want to reference.  Isn't there a way to add the whole library and just select with #includes which parts I need?

I'm attaching the code I am using to try and get this all to work just in case that helps.

Thanks for the continued help and advice.  I really appreciate everyone's time/effort.

--Charly
#include "stdafx.h"
 
// include log4cxx header files.
//#include "log4cxx/logger.h"
//#include "log4cxx/basicconfigurator.h"
//#include "log4cxx/helpers/exception.h"
 
#include <logger.h>
#include <basicconfigurator.h>
#include <exception.h>
 
using namespace log4cxx;
using namespace log4cxx::helpers;
 
LoggerPtr logger(Logger::getLogger("MyApp"));
 
int _tmain(int argc, _TCHAR* argv[])
{
        int result = EXIT_SUCCESS;
 
        try
        {
                // Set up a simple configuration that logs on the console.
                BasicConfigurator::configure();
 
                LOG4CXX_INFO(logger, "Entering application.");
                LOG4CXX_INFO(logger, "Exiting application.");
        }
        catch(Exception&)
        {
                result = EXIT_FAILURE;
        }
 
        return result;
}

Open in new window

You need to tell the linker both where the library lives and the library to use. Did you look at this link that I posted above?

http://msdn2.microsoft.com/en-us/library/ms235627(VS.80).aspx

It takes you through creating and using a static library.

This one is for DLLs

http://msdn2.microsoft.com/en-us/library/ms235636(VS.80).aspx

-Rx.
As I wrote above:

------------------------------->8------------------------------
Either copy the libs to a separate 'lib' directory of your project and add the path under your project settings (in the "Linker" section) or you leave them where they are and add that path. The same applies to the header files.
------------------------------->8------------------------------

The important part is to add the path to your project settings for both the .lib and the .h files.
jkr,

I just tried adding the /include directory for referencing the .h files, and the path to the folder containing log4cxxd.lib under Project Property Pages > Linker  > General > Additional Library Dependencies.  Is that correct? If yes, I still can't get the compiler to find logger.h.  Is there anything else I have to enable or change to get it to work?

evilrix,

I did read through the first link you sent me.   It seems like that would be used if I was going to code a library myself from the ground up, not utilize one which is already created.  Is that right, or am I missing something?

--Charly
>> It seems like that would be used if I was going to code a library myself
If you read the tutorial through you'll see it also shows you how to link to a library once you've written one -- this was the main reason for me pasting you the link... so you could read that part!

-Rx.
Well, you'd set "Configuration Properties|C++|Genera|Additional Include Directories" to point to the directory where the log4cxx headers reside and add the libs via the IDE, then that should work. If you have chosen the #pragma approach, you'd need to add the lib directory under "Configuration Properties|Linker|Genera|Additional Library Directories"
evilrix,

After reading the part you suggested (sorry I wasn't thorough enough before) I got it to build, but with some errors.

jkr,

After your clarifications I got it to build this way as well.

The problem is, with both of them I am getting some build errors (same errors).  They look DLL related, even though I was using the static library method...  I'll post them below.

Thanks for your continued patience with all this.

--Charly
1>Testing.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall log4cxx::Logger::forcedLog(class log4cxx::helpers::ObjectPtrT<class log4cxx::Level> const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class log4cxx::spi::LocationInfo const &)" (__imp_?forcedLog@Logger@log4cxx@@QAEXABV?$ObjectPtrT@VLevel@log4cxx@@@helpers@2@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABVLocationInfo@spi@2@@Z) referenced in function _wmain

Open in new window

Hm, are you sure you added the library?
I'm pretty sure.  It shows up in solution explorer under my project...

Project > Add Existing Item > "build\debug\static\log4cxxd.lib"

Now when I try and add it to the project itself, the filter doesn't automatically include .lib files.  I have to set the filter to all files to be able to add it in there.  On the other hand, when I try to add it to the solution, the file filter automatically lets you choose .lib files.  Does it matter if I add it at the project or solution level?

Does it matter that it has the extra "d" on the end of it?  I assume it's for debug, but could that throw off the linker somehow?

--Charly
>>Does it matter if I add it at the project or solution level?

Yes, you should add the lib to the project, not to the solution.
Ok, that's how I have it as of now, so I'll leave it be.
Is log4cxx the best logging solution available for C++ development?  

What do you guys use to perform logging on your applications?  I don't need anything real fancy, just console logging and log file generation for a small app which is going to run on a Server 2000 machine.

I'm starting to wonder if I'm going down the right path for my problem...

--Charly
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I ended up using a simpler logger as you  suggested.  My application isn't large enough to require all the power of log4cxx...

Thanks for the help everyone.

--Charly
You're most welcome ;o)