public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* #line directive and -D option
@ 2008-12-16  6:11 Hiroki Kaminaga
  2008-12-16 12:45 ` John (Eljay) Love-Jensen
  0 siblings, 1 reply; 7+ messages in thread
From: Hiroki Kaminaga @ 2008-12-16  6:11 UTC (permalink / raw)
  To: gcc-help


Hi,

When #line directive and -D NAME=DEFINITION is used togather, I get
a compile error. Below is what I tried:

$ cat macro-test.c 
#if !defined(MYNAME)
#define MYNAME __FILE__
#endif
#line 5 MYNAME
#include <stdio.h>
int main(void)
{
  printf ("%s: .... \n", __FILE__);
  return 0;
}

$ cat Makefile 
CROSS=
CC=$(CROSS)gcc
all: 
	$(CC) $(shell pwd)/macro-test.c -o macro-test
	$(CC) $(shell pwd)/macro-test.c -o macro-test2 -D MYNAME="macrotest"

$ make
gcc /home/kaminaga/tmp/dir/macro-test.c -o macro-test
gcc /home/kaminaga/tmp/dir/macro-test.c -o macro-test2 -D MYNAME="macrotest"
/home/kaminaga/tmp/dir/macro-test.c:4:9: "macrotest" is not a valid filename
make: *** [all] Error 1

I'm using (old) FC3 gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3).
I get same result on CentOS 5 gcc version 4.1.2 20070626 (Red Hat 4.1.2-14)


From gcc info, -D option states

`-D NAME=DEFINITION'
     The contents of DEFINITION are tokenized and processed as if they
     appeared during translation phase three in a `#define' directive.
     In particular, the definition will be truncated by embedded
     newline characters.

I don't know much about above "translation phase three", and when #line
is processed, so is this a bug or GCC constraint?


Thanks in Advance,

(Hiroki Kaminaga)
t
--

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: #line directive and -D option
  2008-12-16  6:11 #line directive and -D option Hiroki Kaminaga
@ 2008-12-16 12:45 ` John (Eljay) Love-Jensen
  2008-12-17  2:39   ` Hiroki Kaminaga
  0 siblings, 1 reply; 7+ messages in thread
From: John (Eljay) Love-Jensen @ 2008-12-16 12:45 UTC (permalink / raw)
  To: Hiroki Kaminaga, gcc-help

Hi Hiroki,

Worked on my machine.

I think the problem is that you are not passing this to the compiler:
-D MYNAME="macrotest"

Rather, I think you are passing this to the compiler:
-D MYNAME=macrotest

Depending on your shell, you may need to escape the quotes, such as one of these two ways:
-D MYNAME=\"macrotest\"
-D MYNAME='"macrotest"'

OTHERWISE, I suspect, your shell is processing (stripping) the quotes.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: #line directive and -D option
  2008-12-16 12:45 ` John (Eljay) Love-Jensen
@ 2008-12-17  2:39   ` Hiroki Kaminaga
  2008-12-17  2:40     ` Jan Engelhardt
  2008-12-17  3:24     ` John (Eljay) Love-Jensen
  0 siblings, 2 replies; 7+ messages in thread
From: Hiroki Kaminaga @ 2008-12-17  2:39 UTC (permalink / raw)
  To: eljay; +Cc: gcc-help


Hi,

From: "John (Eljay) Love-Jensen" <eljay@adobe.com>
> Hi Hiroki,
> 
> Worked on my machine.
...
> Depending on your shell, you may need to escape the quotes, such as one of these two ways:
> -D MYNAME=\"macrotest\"
> -D MYNAME='"macrotest"'

Ah, above worked, thank you!


By the way, is there any method to strip strings prefix passed to
__FILE__ macro?  If .c file is specified by abosolute path, __FILE__
also prints absolute path:

$ cat macro-test.c 
#include <stdio.h>
int main(void)
{
  printf ("%s: .... \n", __FILE__);
  return 0;
}
$ gcc `pwd`/macro-test.c -o macro-test
$ ./macro-test
/home/kaminaga/tmp/dir/macro-test.c: .... 

and I was looking for e.g. environment variable that would strip
prefix such as in patch -p option...


Best Regards,

(Hiroki Kaminaga)
t
--

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: #line directive and -D option
  2008-12-17  2:39   ` Hiroki Kaminaga
@ 2008-12-17  2:40     ` Jan Engelhardt
  2008-12-17  3:24     ` John (Eljay) Love-Jensen
  1 sibling, 0 replies; 7+ messages in thread
From: Jan Engelhardt @ 2008-12-17  2:40 UTC (permalink / raw)
  To: Hiroki Kaminaga; +Cc: eljay, gcc-help


On Wednesday 2008-12-17 02:56, Hiroki Kaminaga wrote:
>Hi,
>
>From: "John (Eljay) Love-Jensen" <eljay@adobe.com>
>> Hi Hiroki,
>> 
>> Worked on my machine.
>...
>> Depending on your shell, you may need to escape the quotes, such as one of these two ways:
>> -D MYNAME=\"macrotest\"
>> -D MYNAME='"macrotest"'
>
>Ah, above worked, thank you!
>
>By the way, is there any method to strip strings prefix passed to
>__FILE__ macro?  If .c file is specified by abosolute path, __FILE__
>also prints absolute path:

Well use a function that returns the basename. Something like

	/* from libHX-1.28/src/string.c */
	EXPORT_SYMBOL char *HX_basename(const char *s)
	{
		const char *p;
		if ((p = strrchr(s, '/')) != NULL)
			return const_cast1(char *, p + 1);
		return const_cast1(char *, s);
	}

and

>$ cat macro-test.c 
>#include <stdio.h>
>int main(void)
>{
>  printf ("%s: .... \n", __FILE__);
   printf("%s: ...\n", HX_basename(__FILE__));
>  return 0;
>}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: #line directive and -D option
  2008-12-17  2:39   ` Hiroki Kaminaga
  2008-12-17  2:40     ` Jan Engelhardt
@ 2008-12-17  3:24     ` John (Eljay) Love-Jensen
  2008-12-17  3:55       ` Jan Engelhardt
  1 sibling, 1 reply; 7+ messages in thread
From: John (Eljay) Love-Jensen @ 2008-12-17  3:24 UTC (permalink / raw)
  To: Hiroki Kaminaga; +Cc: gcc-help

Hi Hiroki,

The __FILE__ is based on what you pass.

gcc -c foo.c
__FILE__ is "foo.c"

gcc -c ././././foo.c
__FILE__ is "././././foo.c"

If you don't want the prefix, don't append the prefix.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: #line directive and -D option
  2008-12-17  3:24     ` John (Eljay) Love-Jensen
@ 2008-12-17  3:55       ` Jan Engelhardt
  2008-12-17 17:47         ` Hiroki Kaminaga
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Engelhardt @ 2008-12-17  3:55 UTC (permalink / raw)
  To: John (Eljay) Love-Jensen; +Cc: Hiroki Kaminaga, gcc-help


On Wednesday 2008-12-17 03:37, John (Eljay) Love-Jensen wrote:

>Hi Hiroki,
>
>The __FILE__ is based on what you pass.
>
>gcc -c foo.c
>__FILE__ is "foo.c"
>
>gcc -c ././././foo.c
>__FILE__ is "././././foo.c"
>
>If you don't want the prefix, don't append the prefix.

Well sometimes that just is not possible; for example when
you do an out-of-srcdir build in an autotools project, aka:

	mkdir obj
	cd obj
	../configure

then the compiler will be invoked with lots of ../${filename}.c.
That's why I use basename.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: #line directive and -D option
  2008-12-17  3:55       ` Jan Engelhardt
@ 2008-12-17 17:47         ` Hiroki Kaminaga
  0 siblings, 0 replies; 7+ messages in thread
From: Hiroki Kaminaga @ 2008-12-17 17:47 UTC (permalink / raw)
  To: jengelh; +Cc: eljay, gcc-help, kaminaga


From: Jan Engelhardt <jengelh@medozas.de>
...
> >If you don't want the prefix, don't append the prefix.
> 
> Well sometimes that just is not possible; for example when
> you do an out-of-srcdir build in an autotools project, aka:
> 
> 	mkdir obj
> 	cd obj
> 	../configure
> 
> then the compiler will be invoked with lots of ../${filename}.c.
> That's why I use basename.

Some part of prefix is needed if there is a same filename in different
direcotry, so I wanted to strip top N prefix, not all dirname. However,
I think this is more of a build environment issue, and if there were any
convenient gcc functionlarity, I wanted to use it.


Best Regards,
(Hiroki Kaminaga)
t
--

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-12-17  3:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-16  6:11 #line directive and -D option Hiroki Kaminaga
2008-12-16 12:45 ` John (Eljay) Love-Jensen
2008-12-17  2:39   ` Hiroki Kaminaga
2008-12-17  2:40     ` Jan Engelhardt
2008-12-17  3:24     ` John (Eljay) Love-Jensen
2008-12-17  3:55       ` Jan Engelhardt
2008-12-17 17:47         ` Hiroki Kaminaga

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).