Operating System - OpenVMS
1828359 Members
3097 Online
109976 Solutions
New Discussion

Any way to do token pasting in #include directive?

 
SOLVED
Go to solution
Scott Mark
Occasional Contributor

Any way to do token pasting in #include directive?

I'm using Compaq C, version 6.5-001 on OpenVMS Alpha V7.3-2. It's not something I can change.

I'm trying to create a unit test driver that can include a particular unit, and a particular unit test, as in:

// driver.c
#include "unit.c"

// library functions

#include "test_unit.c"
---

except that I want unit.c and test_unit.c to be parameterized. I've tried all sorts of things, but it looks like the "no recursive macro expansion" is going to kill the idea.

I've tried the following:

// driver.c
#include foo
--

cc /define="foo=unit.c" driver.c

and that doesn't work.


The following DOES work:

// driver.c
#define foo "unit.c"
#include foo

My problem is that, for the life of me, I can't figure out how to get the double quotes to surround the file name.

I'm trying to build a .COM file that can compile a particular unit. Please forgive the following pseudo-code- I haven't used VMS for 20 years, so I have to relearn DCL.

$ unit=fn_foo
$ un_inc="unit_"+$unit+".h"
$ un_src=$unit+".c"
$ un_drv="driver_"+$unit+".h"
$ cc /define="hdr=$uninc,code=$un_src,driver=$un_drv"
driver.c

Something. ANYTHING. But that above example still won't get the double-quotes into the defined string.

That's what's really driving me crazy. I can't use any kind of token pasting or other manipulation that the compiler will accept to turn foo.c into "foo.c" within a #include statement.

I've searched the forums, (I think. I hope) and I didn't find anything on "token pasting".

Is there another solution? Is there a simple way to embed a double quote character in a /define="stuff" statement?
7 REPLIES 7
Bojan Nemec
Honored Contributor
Solution

Re: Any way to do token pasting in #include directive?

Mark,

Try with :
cc /define="foo=""unit.c""" driver.c

I tested and works. Maybe a more VMS solution;)

$ define foo unit.c
$ cc driver.c

Where in driver.c you have:
#include "foo"
.
.
.

Bojan
Ian Miller.
Honored Contributor

Re: Any way to do token pasting in #include directive?

have you tried defining FOO as a logical name?
$ DEFINE FOO "UNIT.C"

I don't know if the C compiler will do the right thing.
____________________
Purely Personal Opinion
Joseph Huber_1
Honored Contributor

Re: Any way to do token pasting in #include directive?


As usual in DCL: quote the quotes.
Example:
cc/define=(FOO="""XXX.H""") sys$input
#include FOO

Will include file XXX.H .
http://www.mpp.mpg.de/~huber
Scott Mark
Occasional Contributor

Re: Any way to do token pasting in #include directive?

Wups! I guess I'm not done with this one.

The one technique that worked for me:

cc /define="foo=""unit.c""" driver.c

All the ones using $ define foo unit.c didn't work, although admittedly this was on the command line, and not in a .com file.

The fact that I'm still in this thread is my own fault. I need to define more than one string.

I've tried:

cc /define="foo=""unit.c"",bar=""unit.h""" driver.c

I get complaints about extra characters.

I've tried

cc /define=(foo=""unit.c"",bar=""unit.h"") driver.c

I get complaints about Unable to find unit as if the extension were being dropped.


I suppose this whole thing boils down to constructing strings and escaping quotes in DCL, but it just seems that there's no logical progression. The help says that for two defines, it should be:

/define="a=b,c=d" or something like that. But if you want quotes around b and d:

/define "a=""b"",c=""d""" it doesn't work.

In order to avoid messing up a second time by not supplying enough information, let me reiterate my goal: I want to be able to define "unit" as "fn_foo" and then build three strings around ''unit' and include three files whose names are constructed around ''unit', as shown in the original posting.

Much appreciation for sticking with me through this. I wish I knew why it wasn't possible to string multiple defines together in an obvious way.
Dale A. Marcy
Trusted Contributor

Re: Any way to do token pasting in #include directive?

I think what you are looking for is:

cc /define=("foo=""unit.c""","bar=""unit.h""") driver.c

The rules for quotes are that two consecutive quotes inside a string will output a one quote in the string. The outer most quotes delimit the string.
Bojan Nemec
Honored Contributor

Re: Any way to do token pasting in #include directive?

Mark,

For more than one use:

/define=("a=""b""","c=""d""")

Explanation:
First you must include the definitions in quotes because of DCL, which converts all unquoted strings in uppercase. So without quotes yyou realy receive:
/DEFINE=(A=....
Second when you want to put a single quote in a quoted string you substitute the quote with two quotes:

"a=""b""" becomes a="b"

Bojan
Scott Mark
Occasional Contributor

Re: Any way to do token pasting in #include directive?

Hallelujah! It compiles!

Now to do the DCL to build the three file names from the one unit name.