Solved

Using log4cplus in Visual Studio 2005

Posted on 2007-11-15
32
3,312 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:dleehanson
  • 12
  • 10
  • 9
  • +1
32 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20292481
When you say import are you referring to a static library, a dynamic library or a source code library?
0
 
LVL 1

Author Comment

by:dleehanson
ID: 20292531
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20292534
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20292549
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20292561
Would not be much different than Log4Net

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

Best Regards,
DeepuAbrahamK
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20292571
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
 
LVL 86

Expert Comment

by:jkr
ID: 20292579
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
 
LVL 1

Author Comment

by:dleehanson
ID: 20292645
Reading...

--Charly
0
 
LVL 86

Expert Comment

by:jkr
ID: 20292737
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
 
LVL 1

Author Comment

by:dleehanson
ID: 20292970
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20293009
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
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 135 total points
ID: 20293024
0
 
LVL 1

Author Comment

by:dleehanson
ID: 20293047
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20293069
Have you built the source? If so try using Windows search from the top folder of the log4cxx project I guess :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 20293096
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
 
LVL 1

Author Comment

by:dleehanson
ID: 20293120
Thanks guys.  I'll check out the link to that walkthrough evilrix sent me and begin the battle anew tomorrow!

--Charly
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 40

Expert Comment

by:evilrix
ID: 20293137
>>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
 
LVL 1

Author Comment

by:dleehanson
ID: 20298960
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20299036
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
 
LVL 86

Expert Comment

by:jkr
ID: 20299150
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
 
LVL 1

Author Comment

by:dleehanson
ID: 20299301
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20299317
>> 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
 
LVL 86

Expert Comment

by:jkr
ID: 20299326
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
 
LVL 1

Author Comment

by:dleehanson
ID: 20299664
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
 
LVL 86

Expert Comment

by:jkr
ID: 20299699
Hm, are you sure you added the library?
0
 
LVL 1

Author Comment

by:dleehanson
ID: 20299846
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
 
LVL 86

Expert Comment

by:jkr
ID: 20299922
>>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
 
LVL 1

Author Comment

by:dleehanson
ID: 20299937
Ok, that's how I have it as of now, so I'll leave it be.
0
 
LVL 1

Author Comment

by:dleehanson
ID: 20300346
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
 
LVL 86

Accepted Solution

by:
jkr earned 165 total points
ID: 20300829
>>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
 
LVL 1

Author Comment

by:dleehanson
ID: 20315446
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
 
LVL 86

Expert Comment

by:jkr
ID: 20315464
You're most welcome ;o)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now