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

前端 未结 6 768
春和景丽
春和景丽 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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)
    

提交回复
热议问题