quinta-feira, 28 de abril de 2011

How to build LOG4CXX and link it statically in Visual Studio 2010

As it is so hard to properly find this information (and it would have saved me about a day of frustrating work), here is how I did solved the problem (I guess...):

STEP 1) TO COMPILE LIB4CXX AS A STATIC LIBRARY....

1) Just open the log4cxx project/solution already sent with the downloaded source files....
2) Go the log4cxx project, and open its properties dialog...
3) Select Configuration Properties->General and set Configuration Type to Static Library (.lib)
4) Go to Configuration Properties->Preprocessor and add to Preprocessor Definitions the following (LOG4CXX_STATIC) ---> this is the only thing that I read everywhere, but it is not enough...
5) Go to Configuration Properties->Code Generation and set RunTime Library to Multi-threaded Debug (/MTd) --> with you want a release (without debug option), you have to change this option... I do not know if it is really necessary, but aldo DO SET the same property for apr, apr_util and xml...
6) Build log4cxx
7) My result is a file named log4cxx.lib with about 37MB of space... (I have seen another option of the lib with only 15MB... why? I REALLY do not know...)

STEP 2) HOW TO USE LIB4CXX AS A STATIC LIBRARY

If you usually program in C++ for Linux as I do, this is quite interesting (and I feel like really stupid after discovering it): you have to list all (I mean ALL) static libraries need for log4cxx.lib... this is just not necessary for g++, as it has all libs in one place (usually) and is quite capable of searching for those dependency libraries.... but guess what... VC++ does not do that...
So...
1) First of all... you project has to match the log4cxx version.. that means, if you use a /MTd definition while building the lib, you do have to use the same option with your project... So, go to Configuration Properties->Code Generation and set RunTime Library to Multi-threaded Debug (/MTd) or any other option you want (in the case of a static lib, only /MT...)
2) Second of all, as it is found in some messages, you do have to add LOG4CXX_STATIC to Preprocessor Definitions of your project also...
3) This was my error.... you do have to explicit all paths and libs you use, so add the path to log4cxx.lib (obvious), but also to apr-1.lib, aprutil-1.lib, xml.lib...
4) Add all libs you need (Linker->Input->Additional Dependencies) and DO ADD the following: WS2_32.Lib, MsWSock.Lib, AdvAPI32.Lib, odbc32.lib (this is already said in the list)... AND ALSO ADD log4cxx.lib - this is what you want, after all, but also add the apr, apr_util and xml static libraries... in my case: apr-1.lib, aprutil-1.lib, xml.lib
Build your project, and it should work... (if I did not forgot anything else...).

I will try to elaborate this a little, but I guess something like that should be inserted in log4cxx wiki...

If you want an easier solution (yesterday I got this, but I was not happy with the fact that I was unable to do it myself):
http://www.dreamcubes.com/b2/software-development/28/log4cxx-for-win32-with-vs2005/

Also, I guess it should be said: if you want to debug you project, you need a debug log4cxx.lib version, if you want to release it, you need a release version of the lib, and so on...
Also, as the lib uses a lot of C++, a lib version for VS2003 will not work in VS2008, cause there is a lot of changes from STL from VS2003 to VS2008.... you do need to compile it using the same compile version... (as I searched in the web, this is true for most of the static libraries at least with VC++).

If I said anything or a lot of things that is wrong, please correct me :) :)