How to force inclusion of an object file in a static library when linking into executable?

前端 未结 6 755
春和景丽
春和景丽 2020-12-02 23:10

I have a C++ project that due to its directory structure is set up as a static library A, which is linked into shared library B, which is linked in

相关标签:
6条回答
  • 2020-12-02 23:48

    There is a better way to write that FORCE_UNDEFINED_SYMBOL macro. Just cast that function pointer to a void*. Then it works with any function - or data for that matter. Also, why bother with MSVC pragmas when the gcc part of your macro will work for MSVC as well. So my simplified version would be:

    #define FORCE_UNDEFINED_SYMBOL(x) void* __ ## x ## _fp =(void*)&x;
    

    Which is used thusly:

    FORCE_UNDEFINED_SYMBOL(Af)
    

    But it must be used in the program that includes the library that is having its symbols stripped.

    0 讨论(0)
  • 2020-12-02 23:49

    MSVC #pragma comment(linker, "/include:__mySymbol")

    gcc -u symbol

    0 讨论(0)
  • 2020-12-02 23:49

    If you can use C++0x features of gcc (-std=c++0x), then the function default template arguments may do the trick. As of the current c++ standard, default arguments are not allowed for function templates. With these enabled in c++0x, you can do something like :-

    In some header file of static library ...

    template< class T = int >
    void Af()
    {
    }
    

    Then in its corresponding cpp file use explicit template instantiation...

    template void Af();
    

    This will generate the symbols for the function Af though it is not yet called/referenced. This won't affect the callers due to the fact that because of the default template argument, you need not specify a type. Just add the template <class T = int > before the function declaration and explicitly instantiate it in its implementation file.

    HTH,

    0 讨论(0)
  • 2020-12-02 23:50

    You can use the --undefined option when you build B:

    g++ -Wl,--undefined,Af -o libB.so ...
    
    0 讨论(0)
  • 2020-12-02 23:52

    It turns out my original attempt was mostly there. The following works:

    extern "C" void Af(void);
    void (*Af_fp)(void) = &Af;
    

    For those that want a self-contained preprocessor macro to encapsulate this:

    #if defined(_WIN32)
    # if defined(_WIN64)
    #  define FORCE_UNDEFINED_SYMBOL(x) __pragma(comment (linker, "/export:" #x))
    # else
    #  define FORCE_UNDEFINED_SYMBOL(x) __pragma(comment (linker, "/export:_" #x))
    # endif
    #else
    # define FORCE_UNDEFINED_SYMBOL(x) extern "C" void x(void); void (*__ ## x ## _fp)(void)=&x;
    #endif
    

    Which is used thusly:

    FORCE_UNDEFINED_SYMBOL(Af)
    
    0 讨论(0)
  • 2020-12-03 00:10

    Try putting those lines into B/initB.cpp so that they're (hopefully) forced into the libB.so library at link time.

    But why do you have to do it in this way at all? Can't you set it up so that the executable references that function (or a caller of it), causing the linker to do the right thing automatically?

    0 讨论(0)
提交回复
热议问题