Cpp + Python: static data dynamic initialization in *nix shared lib?

Alf P. Steinbach /Usenet alf.p.steinbach+usenet at gmail.com
Tue Jul 13 15:53:09 EDT 2010


* Jonathan Lee, on 13.07.2010 16:41:
>> Problem (C) is outside the realm of the C++ standard, since the C++ standard
>> doesn't support shared libraries, and I've never actually used *nix shared
>> libraries so I don't /know/...
>>
>> Is such dynamic initialization guaranteed?
>>
>
> Not guaranteed, though I think there's a combination of dlopen options
> and gcc command line parameters that invoke this behavior. See the
> second page of
>
>     http://www.linuxjournal.com/article/3687
>
> about auto-registration.
>
> Personally, though, it never worked for me :/

Ah, well. :-(  Thanks for the info! OK, I'll just have to replace the 
auto-registration with some C++ magic. For which I think I'll simply /require/ 
that the compiler supports mixing of C and C++ linkage, that is, that ...


<code language="Not quite standard C++!">
     #include <iostream>

     extern "C"
     {
         typedef int (*Callback)( int );
     }

     void foo( Callback f ) { std::cout << "foo!" << f( 42 ) << std::endl; }

     int a( int ) { return 1; }
     extern "C" int b( int ) { return 2; }

     int main()
     {
         foo( a );       // Unholy Mix of C++ and C linkage, formally not OK.
         foo( b );       // Should be OK with any compiler.
     }
</code>


... compiles, and works.


Cheers, & thanks,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>



More information about the Python-list mailing list