From: ratheesh kannoth <ratheesh.ksz@gmail.com>
To: Zhongye Jia <jia.zhongye@gmail.com>
Cc: Michal Nazarewicz <mina86@mina86.com>,
linux-c-programming@vger.kernel.org
Subject: Re: macro to print filename
Date: Thu, 2 Jun 2011 23:41:05 +0530 [thread overview]
Message-ID: <BANLkTikKFX=aV4JqRz-5cTMtiUBTS1ni3Q@mail.gmail.com> (raw)
In-Reply-To: <BANLkTin7xYkdGqUKFqUp2hu3oktBxOzBgg@mail.gmail.com>
On Thu, Jun 2, 2011 at 7:09 PM, Zhongye Jia <jia.zhongye@gmail.com> wrote:
> thanks very much! it works very well only except that the
> initialization should be placed out of the struct. the compiling time
> computation in c++ is really amazing.
>
> 2011/6/2 Michal Nazarewicz <mina86@mina86.com>:
>> On Wed, 01 Jun 2011 20:45:06 +0200, Zhongye Jia <jia.zhongye@gmail.com>
>> wrote:
>>>
>>> I do not think this can be done in *compiling* time, neither macro in
>>> c nor template in c++, so would you be so kind to offer the solution
>>> by using template or just give some tips? Since I'm not that good at
>>> c++ :)
>>
>> I was thinking along the lines of:
>>
>> template<unsigned offset>
>> struct _filename {
>> static const char *const val = __FILE__[offset] == '/'
>> ? __FILE__ + offset + 1
>> : _filename<offset-1>::val;
>> };
>>
>> template<>
>> struct _filename<0> {
>> static const char *const val = __FILE__;
>> };
>>
>> static const char *const filename = _filename<sizeof(__FILE__) - 1>;
>>
>> I'm a bit rusty so I dunno if that'll work in this format.
>>
>> --
>> Best regards, _ _
>> .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o
>> ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
>> ooo +-----<email/xmpp: mnazarewicz@google.com>-----ooO--(_)--Ooo--
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
This is nice one.
so we could use __FILE__ with Makefile to solve this problem. We could
use __FILE__ in our program and define Makefile for each directory.
From toplevel directory, call using - make -C subdir/ . This will give
help __FILE__ macro to exapand to just filename since "make" is
executing inside the directory.
-Ratheesh
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2011-06-02 18:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-01 16:07 macro to print filename ratheesh kannoth
2011-06-01 17:38 ` Michal Nazarewicz
2011-06-01 18:45 ` Zhongye Jia
2011-06-01 19:08 ` Michal Nazarewicz
2011-06-02 13:39 ` Zhongye Jia
2011-06-02 18:11 ` ratheesh kannoth [this message]
2011-06-02 19:11 ` Michal Nazarewicz
2011-06-01 18:08 ` Zhongye Jia
2011-06-01 19:11 ` Michal Nazarewicz
2011-06-01 19:24 ` Aniruddha Bhattacharyya
2011-06-01 19:27 ` Michal Nazarewicz
2011-06-02 2:10 ` Libin Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='BANLkTikKFX=aV4JqRz-5cTMtiUBTS1ni3Q@mail.gmail.com' \
--to=ratheesh.ksz@gmail.com \
--cc=jia.zhongye@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=mina86@mina86.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 NNTP newsgroup(s).