Emscripten does some extra dead-symbol elimination which seems to kill the
static initializer when linking in a separate .a library archive.

You can pass the -s LINKABLE=1 option to disable dead-symbol elimination,
but this may increase the total size of the resulting output... (I think
it's including the whole C standard library?)

If you can, try building the 'plugin' part to an .so or .o file instead of
an .a static library, it seems to be happier linking that way and doesn't
bloat the output file. By building a libLib.so instead of libLib.a, I can
successfully link your demo and have the plugin bit run without any special
linker options. (.so libs will be statically linked)

-- brion


On Wed, Jan 11, 2017 at 12:34 AM, Andrew S. <[email protected]>
wrote:

>
> Steps to reproduce:
>
> *Main.cpp:*
>
> static void (*g_pFunc)() = 0;
>
> void Register(void (*pFunc)())
> {
> g_pFunc = pFunc;
> }
>
> int main()
> {
> if (g_pFunc)
> g_pFunc();
>
> return 0;
> }
>
> *Lib.cpp:*
>
> #include <stdio.h>
>
> extern void Register(void (*pFunc)());
>
> static void PrintFunc()
> {
> printf("Hello from lib\n");
> }
>
> struct CRegistrator
> {
> CRegistrator()
> {
> Register(&PrintFunc);
> }
> };
>
> static CRegistrator g_Registrator;
>
> *Compile with Emscripten:*
>
> em++ -c -o Lib.o Lib.cpp
> emar rcs libLib.a Lib.o
> em++ Main.cpp -Wl,--whole-archive libLib.a -Wl,--no-whole-archive -o a.html
>
> *a.html should print "Hello from lib", but it doesn't.*
>
> *Compile with GCC:*
>
> g++ -c -o Lib.o Lib.cpp
> ar rcs libLib.a Lib.o
> g++ Main.cpp -Wl,--whole-archive libLib.a -Wl,--no-whole-archive -o a.out
>
> *a.out prints "Hello from lib" as expected.*
>
> Hi!
>>
>> I need to link an executable against a static library which is using only
>> static variables for its registration (this is actually a plugin).
>>
>> Usually -force_load (for Clang) or --whole-archive (for GCC) are used to
>> force a library to link. But it seems link Emscripten just ignores these
>> flags.
>>
>> Any advice?
>>
>> Andrew.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to