help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Running makefiles from emacs


From: mrf
Subject: Re: Running makefiles from emacs
Date: Thu, 01 Jul 2021 12:33:48 +0300
User-agent: mu4e 1.5.8; emacs 27.2

Arthur Miller writes:

> Emacs comment is neat. You can achieve similar with a small shell script
> too, check this:
>
> https://github.com/ryanmjacobs/c

I love this community it's nice place to share nice hacks

But I have something surpasses this and I don't use it in my article
because it's too old, anyway take this:

/*****************/
/* script.c file */
/*****************/
#if 0
#//if file is not yet compiled
if [ -f $(basename $0 .c) ]
then
  printf ""
else
  gcc $CFLAGS $CXXFLAGS $LDFLAGS $0 -o `basename $0 .c` && exec ./`basename $0 
.c` "$@";
fi;

#//if file is already compiled
if [ `stat -c %Y $(basename $0 .c) ` -lt `stat -c %Y $0` ]
then
gcc $CFLAGS $CXXFLAGS $LDFLAGS $0 -o `basename $0 .c` && exec ./`basename $0 
.c` "$@";
else
  exec ./`basename $0 .c` "$@";
    fi;
exit
#endif // 0

#include <stdio.h>
#include <math.h>
    
int main (int argc, char **argv)
{
  printf("%f\n", cosf (103.0f));

  printf(
    "variables in argv (excluding %s) =%d\nparam1=%s param2=%d param3=%d\n\n",
    __FILE__, argc-1, argv[1], atoi(argv[2]), atoi(argv[3]));

  return 0;
}

/* Local Variables: */
/* compile-command: "LDFLAGS='-lm' CFLAGS='-Wall' bash script.c 'String' 10 
5395" */
/* End: */




this neat hack will include a full fledged bash script and this script
will only compile when file is changed otherwise will run the program,
you can run it with:

bash script_name.c

or by changing the file permission `chmod +x script_name.c` and then
`./script_name.c`

the idea is that there is conditional preprocessor that will only
compile when true(1) and we set it to false(0) on purpose.

Also this script allow you to pass paramenters and also compiler flags
by using environment variable and I think this is great way to write C
CGI scripts.

And BTW tiny C compiler is better option than what you send.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]