ANSI C library for Aspect-Oriented Programming - c

I'm searching for a good ANSI C library for Aspect-Oriented Programming.
Some desired features are:
Accessing and modifying arguments of the target function.
Making the target function return and controlling the return value.
I found aspeCt C (https://sites.google.com/a/gapp.msrg.utoronto.ca/aspectc/home), reading the documentation it seems to have everything I need, but when, following the instructions, I run make to compile and pass the tests, the tests fail.
There is any alternative?

You can try AspectC++
is a project that extends the AspectJ approach to C/C++.
For example if you want to a simple C program using Aspect:
int main() {
printf("world");
}
And then you will have an aspect.cc
before(): execution(int main()) {
printf("Hello ");
}
after(): execution(int main()) {
printf(" from AspectC ! \n");
}
You compile both with > acc hello.ac world.mc
And the result is:
gcc hello.c world.c
>./a.out
Hello world from AspectC !

Related

How to link a lib written in D to use it with a program written in C, under Windows, using MinGW GCC?

I would like to use a library written in D for a C program compilable with MinGW GCC, for Windows. Here are the codes:
dll.d
extern (C) int dsquare(int n) nothrow
{
return n * n;
}
main.c
#include <stdio.h>
int main()
{
int res = dsquare(6); // Expect '36'
printf("res = %d\n", res);
return 0;
}
There is a tutorial on D's site, but it seems to target only Linux. Indeed, no explanation is given for creating such a dynamic D library for Windows and MinGW users.
D's documentation also says that the -shared option should generate a DLL version of the D code, but in my case, it generates an executable, I don't know why.
Also, anything that seems to generate files to be linked targets MVSC formats and nothing seems to be suitable for MinGW GCC compilers.
So, how can I generate a "GCC-friend" DLL with D, so that I can link it to my C program without having to use another compiler, such as GDC or LDC, via gcc main.c -o main -ldll -L. (I guess)?
I attached link with short explanation. Link D onto C is not so straightforward as C to D. Check D.org page here:
https://dlang.org/spec/betterc.html

Turbo C++ GCC Compiler

I'm currently developing a gui application in turbo c++ (mandatory) for a project using C language, and i need to invoke turbo c++'s compiler(gcc), my question is HOW DO I CALL IT? i can't find any sources in the internet regarding this.
here's a snippet of my code
int program(){
int opt=-1,j;
char menu[4][20]={"Open File","Compile", "Run","Quit"};
close=1;
dropmenu(menu,10,33,131,105,opt);
do{
showmouse();
getmousepos(&buttonm,&xm,&ym);
if(xm>=10+10&&xm<=131-10&&ym>=33+8&&ym<=105-8&&buttonm==1){
for(j=0;j<(105-33-8)/14;j++)
if((ym-33-8)/14==j){
opt=j;
break;
}
dropmenu(menu,10,33,131,105,opt);
switch(opt){
case 0: openFile(); break;
case 1: compile(); break;
case 2: run(); break;
case 3: delay(100);cleardevice();closegraph();exit(0);
}
}else if(xm>=10&&xm<=34&&ym>=18&&ym<=32&&buttonm==1){
dropmenu(menu,10,33,131,105,opt);
continue;
}else if(buttonm==1){
break;
}
}while(close);
return 0;
}
void openFile() {
}
void compile() {
//i would like to put that invoking here
}
void run() {
}
Firstly gcc is not equivalent to turbo-c. turbo uses tc for compiling the codes. You can probably find that from the documentations of turbo c. To compile a file you can use system function. This function helps to execute command on the turbo c shell. I am not sure gcc would be accessible from the turbo shell. Since turbo elevates(mount) the directories giving it restricted permissions only. Also if you want to use the system it would be like following:
system("tc source.c -o destination");
or
system("gcc source.c -o destination");
remember the above code also shows error if any in the text mode only therefore you might want to redirect them to some file. You can find out about redirecting of output of command line from google. There are many resources about it.

Force gcc to use syscalls

So I am currently learning assembly language (AT&T syntax). We all know that gcc has an option to generate assembly code from C code with -S argument. Now, I would like to look at some code, how it looks in assembly. The problem is, on laboratories we compile it with as+ld, and as for now, we cannot use C libraries. So for example we cannot use printf. We should do it by syscalls (32 bit is enough). And now I have this code in C:
#include <stdio.h>
int main()
{
int a = 5;
int b = 3;
int c = a + b;
printf("%d", c);
return 0;
}
This is simple code, so I know how it will look with syscalls. But if I have some more complicated code, I don't want to mess around and replace every call printf and modify other registers, cuz gcc generated code for printf, and I should have it with syscalls. So can I somehow make gcc generate assembly code with syscalls (for example for I/O (console, files)), not with C libs?
Under Linux there exist the macro family _syscallX to generate a syscall where the X names the number of parameters. It is marked as obsolete, but IMHO still working. E.g., the following code should work (not tested here):
_syscall3(int,syswrite,int,handle,char*,str,int len);
// ---
char str[]="Hello, world!\n";
// file handle 1 is stdout
syswrite(1,str,14);

Why does int main() { return main(); } cause stackoverflow and not tail recursion?

Compiled this on Windows using GCC. It crashed immediately with Exception Code: c00000fd.
Edit : Tried compiling following code (for visible output) and it causes stackoverflow.
#include<stdio.h>
int main(void)
{
printf("Hello World\n");
return main();
}
Output -
>gcc trailORoverflow.c -o trailORoverflow.exe
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
It keeps printing Hello World for sometime and crashes.
Edit: Didn't crash with O2, O3 and -O1 -foptimize-sibling-calls optimizations.
The code you have shown will call main infinitely, and therefore will result a stack overflow. This is true in the case of any function and not specific to main. Each function call a stack frame is created in memory, and as infinite such frames is created as the recursion goes deep, you get a stackoverflow.
But if you make a proper base termination like the example as follows, for recursive calls in main then there is an interesting thing.
int main (void)
{
static int x = 100;
if (x == 0)
{
return 0;
}
x--;
printf ("%d\n", x);
main ();
return 0;
}
There is a difference in calling main recursively in C and C++ Language, and I think it is interesting to point that out. Here is a post I wrote, I am giving some explanation about it.
C++ Standards talk about these in
Section 3.6.1 Paragraph 3
Recursive calls are permitted, except to the function named main.
and Section 5.2.2 Paragraph 9
The function main shall not be used within the program. … … …
I did not find any such restriction in the C standards. What I found about recursive calls in the C99 standards in Section 6.5.2.2 Paragraph 11 is as follows
Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions.
Therefore calling main recursively in C Language is deterministic. But as per C++ standards calling main from any function or recursively is not allowed.

Ruby interpreter embed in C code

I just try a simple example from a book:
I have a sum.rb file:
class Summer
def sum(max)
raise "Invalid maximum #{max}" if max < 0
(max*max + max)/2
end
end
And a embed_sum.c file:
#include <stdio.h>
#include <ruby/ruby.h>
int main ( int argc, char ** argv)
{
VALUE result;
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
rb_require("sum");
rb_eval_string("$summer = Summer.new");
rb_eval_string("$result = $summer.sum(10)");
result = rb_gv_get("result");
printf("Result = %d\n", NUM2INT(result));
return ruby_cleanup(0);
}
The I compile it with:
gcc -Wall -lruby -I/usr/include/ruby-1.9.1/ embed_sum.c -o embed_sum
When I launch ./embed_sum it gives me a segmentation fault from the first rb_eval_string.
my version of ruby is : ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux] on Archlinux.
What can be the problem with this example?
The short answer to your problem is to change the line rb_require("sum"); to rb_require("./sum");. This is the change introduced in Ruby 1.9.2 where the current directory is no longer on the load path.
The more general problem is the way embedded Ruby deals with exceptions. The Pickaxe book (which I think is the book you're using, it uses a similar example) has this to say:
If the Ruby code raises an exception and it isn't caught, your C program will terminate. To overcome this, you need to do what the interpreter does and protect all calls that could raise an exception. This can get messy.
You'll need to look into using the rb_protect function to wrap calls to Ruby that might cause an exception. The Pickaxe book has an example of this.

Resources