Re: macro to print filename

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jun 2, 2011 at 7:09 PM, Zhongye Jia <jia.zhongye@xxxxxxxxx> 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@xxxxxxxxxx>:
>> On Wed, 01 Jun 2011 20:45:06 +0200, Zhongye Jia <jia.zhongye@xxxxxxxxx>
>> 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@xxxxxxxxxx>-----ooO--(_)--Ooo--
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> 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@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux