[aspectc-user] multiple definition errors for "global" variables

Olaf Spinczyk Olaf.Spinczyk at informatik.uni-erlangen.de
Wed Feb 2 16:13:41 CET 2005

Hello Hans,

Hans VB wrote:
> Hello,
> I'm writing a logging aspect which logs to a textfile.
> Therefore in my .ah-file I have to define :
> ofstream logfile("test.log");
> For convenience I define this 'global' in the aspect (i.e. before the 
> definition of the aspect LoggingAspect { ... }; )
> This works fine when the advice code only needs to be triggered from one 
> .cpp-file. When I need advice for code scattered across multiple 
> .cpp-files (multiple classes) everything weaves fine, but during linking 
> i get an error from g++ :
> multiple definition of _logfile
> I don't think this is intended behaviour ?
> I managed to work around this by using
> ofstream logfile("test.log",ios::app);
> in every piece of advice-code.
> Greets,
> Hans

the reason is that aspect headers are automatically included in all 
translation units, which are affected by the aspect. You would have the 
same results if you defined the ofstream in a normal header file, which 
you include in more than one cpp file.

The solution is to create a "LoggingAspect.cpp" file, which defines your 
ofstream logfile("test.log") and includes the aspect header explicitely. 
In the aspect header you only have an extern declaration:extern ofstream 


More information about the aspectc-user mailing list