[Interest] Is it safe to call qRegisterMetaType() before main()?

Nikos Chantziaras realnc at gmail.com
Fri Aug 2 09:01:11 CEST 2019


(CC'ing the list as it might be of interest.)

I didn't think volatile would affect the linker. That might indeed work 
if the llvm people think so. I'll try and run some tests when I get to it.

Thanks!


On 02/08/2019 09:54, Dmitriy Purgin wrote:
> Oh, thanks, I didn't get it from the beginning.
> 
> I saw another trick in clang-tidy which apparently does the same thing: 
> declare a volatile variable in global context. See 
> https://github.com/llvm-mirror/clang-tools-extra/blob/master/clang-tidy/llvm/LLVMTidyModule.cpp#L41-L43 
> 
> 
> Cheers
> Dmitriy
> 
> On Fri, Aug 2, 2019 at 8:40 AM Nikos Chantziaras <realnc at gmail.com 
> <mailto:realnc at gmail.com>> wrote:
> 
>     The issue that started the whole discussion is that this sometimes
>     doesn't work when linking statically. The linker might remove
>     unreferenced global objects if you build your common code into a static
>     library that you then link into each application. Example:
> 
>     https://stackoverflow.com/questions/806348/prevent-linker-from-removing-globals
> 
>       From what I can tell, __attribute__((constructor)) won't let that
>     happen.
> 
> 
>     On 02/08/2019 09:32, Dmitriy Purgin wrote:
>      > Hey Nikos,
>      >
>      > as you noticed, the macro expands to a global static object which is
>      > instantiated automatically at program start, thus making it the
>     same as
>      > __attribute__((constructor)) but more portable since it also
>     works on
>      > non-GCC compilers
>      >
>      > Cheers
>      > Dmitriy
>      >
>      > On Fri, Aug 2, 2019 at 2:24 AM Nikos Chantziaras
>     <realnc at gmail.com <mailto:realnc at gmail.com>
>      > <mailto:realnc at gmail.com <mailto:realnc at gmail.com>>> wrote:
>      >
>      >     That macro defines a global object in an unnamed namespace.
>      >     Q_CONSTRUCTOR_FUNCTION(my_function) simply becomes:
>      >
>      >           namespace {
>      >           static const struct my_function_ctor_class_ {
>      >               inline my_function_ctor_class_()
>      >               {
>      >                   my_function();
>      >               }
>      >           } my_function_ctor_instance_;
>      >           }
>      >
>      >     So it doesn't apply any special attributes to the function.
>      >
>      >
>      >     On 01/08/2019 12:18, Dmitriy Purgin wrote:
>      >      > You could also use Q_CONSTRUCTOR_FUNCTION which is
>     probably more
>      >     portable
>      >      >
>      >      > Cheers
>      >      > Dmitriy
>      >      >
>      >      > On Wed, Jul 31, 2019 at 11:23 PM Nikos Chantziaras
>      >     <realnc at gmail.com <mailto:realnc at gmail.com>
>     <mailto:realnc at gmail.com <mailto:realnc at gmail.com>>
>      >      > <mailto:realnc at gmail.com <mailto:realnc at gmail.com>
>     <mailto:realnc at gmail.com <mailto:realnc at gmail.com>>>> wrote:
>      >      >
>      >      >     On 29/07/2019 23:50, Tomas Konir wrote:
>      >      >      > I'm using C constructor attribute with no problems.
>      >      >      > Code like below at each class cpp file.
>      >      >      >
>      >      >      > static void constructor() __attribute__((constructor));
>      >      >      > static void constructor(){
>      >      >      >     qmlRegisterType<ClassName>("ClassName", 1, 0,
>      >     "ClassName");
>      >      >      > }
>      >      >      >
>      >      >      > Maybe it helps.
>      >      >
>      >      >     Thanks! I didn't know about that one.
>      >
> 




More information about the Interest mailing list