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
LVL 1
dleehansonAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
>>Is log4cxx the best logging solution available for C++ development?  

Hard to tell, that will depend on your requirements. But it's definitely a good choice.

>>I don't need anything real fancy, just console logging and log file generation

Then you should be fine with that lib. If yo uneed something simpler, take a look at

http://www.codeproject.com/cpp/loggerservice.asp ("A service to manage and create logfiles")
http://www.codeproject.com/cpp/logdriver.asp ("LogDriver - A simplified log4j using C++")
http://www.codeproject.com/debug/EASYLogger.asp ("Very easy to use logger for ATL/MFC/NonMFC applications")
0
 
evilrixSenior Software Engineer (Avast)Commented:
When you say import are you referring to a static library, a dynamic library or a source code library?
0
 
dleehansonAuthor Commented:
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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
evilrixSenior Software Engineer (Avast)Commented:
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
0
 
evilrixSenior Software Engineer (Avast)Commented:
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Would not be much different than Log4Net

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

Best Regards,
DeepuAbrahamK
0
 
evilrixSenior Software Engineer (Avast)Commented:
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
0
 
jkrCommented:
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
0
 
dleehansonAuthor Commented:
Reading...

--Charly
0
 
jkrCommented:
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.
0
 
dleehansonAuthor Commented:
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
0
 
evilrixSenior Software Engineer (Avast)Commented:
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.
0
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
0
 
dleehansonAuthor Commented:
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
0
 
evilrixSenior Software Engineer (Avast)Commented:
Have you built the source? If so try using Windows search from the top folder of the log4cxx project I guess :)
0
 
jkrCommented:
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.
0
 
dleehansonAuthor Commented:
Thanks guys.  I'll check out the link to that walkthrough evilrix sent me and begin the battle anew tomorrow!

--Charly
0
 
evilrixSenior Software Engineer (Avast)Commented:
>>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
0
 
dleehansonAuthor Commented:
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

0
 
evilrixSenior Software Engineer (Avast)Commented:
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.
0
 
jkrCommented:
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.
0
 
dleehansonAuthor Commented:
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
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
 
jkrCommented:
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"
0
 
dleehansonAuthor Commented:
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

0
 
jkrCommented:
Hm, are you sure you added the library?
0
 
dleehansonAuthor Commented:
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
0
 
jkrCommented:
>>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.
0
 
dleehansonAuthor Commented:
Ok, that's how I have it as of now, so I'll leave it be.
0
 
dleehansonAuthor Commented:
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
0
 
dleehansonAuthor Commented:
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
0
 
jkrCommented:
You're most welcome ;o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.