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

Olaf Spinczyk Olaf.Spinczyk at informatik.uni-erlangen.de
Thu Feb 3 21:05:58 CET 2005


Hello Hans,

Hans VB wrote:

> Hello,
>
> Olaf Spinczyk wrote:
>
>> 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 logfile;
>>
>> Olaf
>
>
> It doesn't give any compiler or weaving errors, but unfortunately, 
> AC++ doesn't seem to go in this LoggingAspect.cpp file. Everything in 
> it is just ignored. It was also the first time I heard of using the 
> .ah-files really just as a header. I'm using the ACDT, could that have 
> something to do with it?
>
> Greets,
> Hans


I just tried logging to an ofstream (in ACDT) and it worked. Here is my 
code:

---main.cc:
#include <stdio.h>

int main () {
    printf ("Hello Hans ;-)");
    return 0;
}
---

---Trace.ah:
#ifndef __Trace_ah__
#define __Trace_ah__

#include <fstream>
using namespace std;

extern ofstream out;

aspect Trace {
    pointcut to_trace () = execution ("% main(...)");
    advice to_trace () : around () {
        out << "before " << JoinPoint::signature () << endl;
        tjp->proceed ();
        out << "after " << JoinPoint::signature () << endl;
    }
};

#endif // __Trace_ah__
---

--- Trace.cc:
#include "Trace.ah"

ofstream out ("logfile");
---

I used an ordinary ACDT managed make project. When I run the compiled 
application the logfile is created as expected.

Please check if there is a relevant difference between my example and 
your code.

Olaf




More information about the aspectc-user mailing list