[R] Re: SOLVED (was Re: using MAKEFLAGS in compiling C code as a shared library using R CMD SHLIB)
faheem at email.unc.edu
Fri Jun 14 07:22:46 CEST 2002
On Thu, 13 Jun 2002, Faheem Mitha wrote:
> Dear R People,
> I'm replying to my own question here. The maintainer of GNU
> Make, Paul Smith, kindly replied to a message I posted to the GNU Make
> help list, help-make at gnu.org.
> I suggest that an example be added to the appropriate section of the FAQ
> to reflect this issue, or perhaps the current example could be expanded.
> Granted, this is just a shell quoting issue, but it might save someone,
> sometime, from some head scratching.
I'm forwarding a further message from Paul Smith, in reponse to a further
query of mine as to *why* this worked, exactly.
I realise this is getting rather off-topic for R, but perhaps people might
be interested not only in the how, but also the why. So I'm sending this
to the R archives for posterity. :-)
Sincerely, Faheem Mitha.
%% Faheem Mitha <faheem at email.unc.edu> writes:
fm> On Thu, 13 Jun 2002, Paul D. Smith wrote:
>> The simplest solution is to use backslashes to quote the spaces:
>> MAKEFLAGS='CC=gcc-3.0 PKG_CFLAGS=\ -Wall\ -pedantic' R CMD SHLIB rc.c -o rc.so
>> will work (note the change to single quotes: if you must use double
>> quotes you'll have to type two backslashes to get one--see the
>> documentation for your shell).
fm> Thanks. This works. Am I correct in thinking this a shell issue?
fm> I am using bash, of course.
The contents of the MAKEFLAGS variable are interpreted by make, not by
However, make uses virtually the same rules for breaking the string up
into words as the one employed by the Bourne shell, except make leaves
out some of the "edge cases".
fm> I am not completely clear on why it works, though. The Bash manual
fm> Single Quotes
fm> Enclosing characters in single quotes (`'') preserves the literal
fm> value of each character within the quotes. A single quote may not occur
fm> between single quotes, even when preceded by a backslash.
fm> Escape Character
fm> A non-quoted backslash `\' is the Bash escape character. It
fm> preserves the literal value of the next character that follows, with
fm> the exception of `newline'. If a `\newline' pair appears, and the
fm> backslash itself is not quoted, the `\newline' is treated as a line
fm> continuation (that is, it is removed from the input stream and
fm> effectively ignored).
fm> So, if single quotes are preserving the literal value of each
fm> character within the quotes, why is it necessary to include a
fm> backslash before each space within single quotes, when a backslash
fm> also "preserves the literal value of the next character that
The single quotes are used so that the shell doesn't do away with your
backslashes. It's easy to see what happens, just do this:
$ echo "foo bar"
$ echo "foo\ bar"
$ echo 'foo\ bar'
Note how the single quotes preserved the backslash in the last example.
You need to get those literal backslashes included in the value that
_make_ sees, because _make_ is going to be chopping up the value of the
MAKEFLAGS variable into words (again, using much the same algorithm as
If make sees the value of MAKEFLAGS is:
foo=bar biz= baz boz= -n
how can it know whether "baz" is supposed to be the value for "biz", or
whether "biz" is being set to the empty value and "baz" is a target to
be built. How can make know whether the "-n" is the make -n option, or
if it was intended to be the value for the variable "boz"?
For that matter, you could want to assign the whole string
"bar biz= baz boz= -n" to the variable "foo"!
A string like that is inherently ambiguous, so make follows the behavior
of the shell and chops it up using spaces as delimiters.
So, if you don't want a space to be treated as a word delimiter, you
have to use a backslash and you have to ensure that _make_ sees the
backslash and that it's not removed by the shell first.
Paul D. Smith <psmith at gnu.org> Find some GNU make tips at:
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help