0

I'm building a little interpreter-like bash shell script to easily run C++ files from the command line. You can see the source code here: https://github.com/binarez/xcpp/blob/master/xcpp.sh. The source files used by my system are .xcpp just to differenciate them from regular cpp files.

It all works pretty good, except when I try to include a xcpp file from another xcpp file : the gcc pre-processor tries to interpret #!, which is the beginning of the shebang line I use to start up my bash script when I run xcpp files.

Note that it works on the "main" file because I use sed to skip over the shebang line before pumping it to gcc but it doesn't work on #include'd file because I don't have a chance to run sed on that included file before the preprocessor processes it.

Exemple of the "main" script test.xcpp:

#!./xcpp.sh

#include "module.xcpp"

int test( strings & args )
{
    module( args );
    return 0;
}

Exemple of module.xcpp:

#!./xcpp.sh

int module( strings & args )
{
    for( string & arg : args )
    {
        println(arg);
    }
    return 0;
}

With this system, you can just chmod +x test.xcpp and run it with ./test.xcpp

I want to be able to also run module.xcpp as a "main" script AND use it within other xcpp scripts. I'm using gcc. How can I make it work?

The error I get:

module.xcpp:1:2: error: invalid preprocessing directive #!
 #!xcpp.sh
binarez
  • 779
  • 1
  • 8
  • 17
  • Not really : I want to build on top of gcc instead of tcc. My xcpp script also does quite a bit : includes all std, using namespace std and adds some custom types and funcs. – binarez Mar 23 '19 at 18:55
  • Many of those answers don't assume TCC. And you can put whatever logic you like in the shell fragment. – o11c Mar 23 '19 at 19:15
  • True, but I would rather not put a 200 lines prelude at the beginning of all my scripts. I was hoping for some gcc pre-processor hack or macro magic :) – binarez Mar 23 '19 at 19:19
  • `#if 0 \n my-script #endif` is only 3 lines. Otherwise there's no solution except *maybe* a GCC plugin. – o11c Mar 23 '19 at 19:21
  • I don't understand why "#if 0 \n my-script #endif" would do what I want to do. I'm still hoping for someone to come along with some macro magic I'm not seeing right now. – binarez Mar 23 '19 at 19:49
  • Because *any* file marked executable without a shebang will run in the shell. So `my-script "$0"` will let you do *anything* when you run the file, without causing problems for GCC. – o11c Mar 23 '19 at 20:04
  • Ok, it works with: `#if 0 \n ./xcpp.sh $0 \n exit \n #endif` But it's way less clean that I had hope. Also, you really that exit there. Why do we need to put the shebang line if it just runs any text file as a bash file?? – binarez Mar 23 '19 at 20:09
  • Or `exec`. Again, there *might* be a "cleaner" solution with plugins, but I'm not sure if plugins are really designed for this kind of thing. – o11c Mar 23 '19 at 20:12
  • For the record, this works pretty cleanly (3 lines): `#if 0 \n . ./xcpp.sh $0 $@ \n #endif` – binarez Mar 23 '19 at 20:22
  • You need double quotes around `"$@"`; otherwise, you will mishandle arguments with quotes or escapes. – tripleee Mar 24 '19 at 10:32

0 Answers0