[aspectc-user] adding aspects to .h and .c files

Olaf Spinczyk Olaf.Spinczyk at informatik.uni-erlangen.de
Fri May 13 11:22:29 CEST 2005


Hi,

Jamal Siadat wrote:
> Hi,
> I have a class "A" declared in "A.h" and implemented in "A.cc". I want to
> add a variable to the class A so I have an aspect adding this variable to
> A.h. I also have an around advice to change the method functionality in
> A.cc.

this should be no problem.

> Both of the aspects compile individually with their respective files But
> together they dont! ( it works if I only try to compile the first aspect
> with A.h or if I try to compile the second aspect with A.cc  one or the
> other but not both together). If I have compiled the first aspect with A.h
> and try to compile the second aspct with A.cc I get a "Segmentation fault"
> messege and the aspect wont compile into A.cc.
> 
> Here it is in c++:
> 
> A.h
> class A {
>  public:
>   int a(const char *name, float b);
> };
> 
> A.cc
> int A::a( const char *name, float b) {
>   jam=7;
>   printf("inside A::a( %f)\n",  b);
>   return jam;
> }
> 
> 1st.ah // to compile with A.h
> 
> aspect Cache  {
> public:
> pointcut shapes() = "A";
> advice shapes() :  int jam;
> };
> 
> 2nd.ah // to compile with A.cc
> 
> aspect A2{
>   advice execution("% A::%(...)") : around()  {
> printf("A: before(exec) %s\n", JoinPoint::signature());
> }};
> 
> When I compile the second aspect "2nd.ah" with A.cc I get "Segmentation
> fault". Is there any way to beat this and fix this error?
> 
> Thanks for your help,

I'm wondering what you mean with "I compile aspect <X> with file <Y>"? 
If you don't have very good reasons and know what you are doing you 
should better compile all translation units of the project (the *.cc 
files) with all aspects defined in the project.

I tried your example with some minor changes (added include guards and 
#include <stdio.h>) and works as aspected. Here is my code:

---A.h---
#ifndef __A_h__
#define __A_h__

class A {
  public:
   int a(const char *name, float b);
};

#endif //  __A_h__
------

---A.cc---
#include "A.h"

int A::a( const char *name, float b) {
   jam=7;
   printf("inside A::a( %f)\n",  b);
   return jam;
}

int main () {
   A a;
   a.a ("hello", 3.14);
}
------

---one.ah---
#ifndef __one_ah__
#define __one_ah__

aspect Cache  {
public:
pointcut shapes() = "A";
advice shapes() :  int jam;
};

#endif //  __one_ah__
------

---two.ah---
#ifndef __two_ah__
#define __two_ah__

#include <stdio.h>

aspect A2 {
   advice execution("% A::%(...)") : around()  {
     printf("A: before(exec) %s\n", JoinPoint::signature());
   }
};

#endif // __two_ah__
------

Save all the files in a directory, cd into the directory and compile 
with "ag++ -o t A.cc". Running "t" yields:

A: before(exec) int A::a(const char *,float)

I hope this helps, although I am not sure what went wrong when you tried it.

-Olaf




More information about the aspectc-user mailing list